DEV Community

Cover image for JS Array Stuffy Stuff
Akira
Akira

Posted on

7 4

JS Array Stuffy Stuff

Honestly, maybe these array methods can help me understand stack. (pushing and popping things off a stack frame, anyone?)

let numbers = [1, 2, 3, 4, 5];

push

Adds an element to the end of an array
numbers.push(6);
numbers
[1, 2, 3, 4, 5, 6]

pop

Pops the last element off of the array and returns it
numbers.pop();
6
numbers
[1, 2, 3, 4, 5]

slice

Slices off whatever you want from index x UP TO index y (slice(x, y)), and returns it.
*Doesn't change the state of the original array
numbers.slice(0, 2);
[1, 2]
numbers
[1, 2, 3, 4, 5]

splice

Deletes elements from index x for the number of places specified by y, returns the deleted element. (ie. array.splice(x, y)

numbers.splice(1, 2);
[2, 3]
numbers
[1, 4, 5]

Top comments (2)

Collapse
 
jadilson12 profile image
Jadilson Guedes β€’

Nice!

Collapse
 
akiramakes profile image
Akira β€’

Thanks!!

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay