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!!