JavaScript array basic operations

In the example, we present push(), shift(), and pop() methods of JavaScript arrays.
Check out all 100 + JavaScript Examples
Check out all JavaScript programs at All JavaScript Programs 

JavaScript array basic operations

The following example presents some basic operations with JavaScript arrays.
const words = [];
words.push("first");
words.push("second", "third", "fourth");

console.log(words);

const el1 = words.shift();
console.log(el1);
console.log(words);

const el2 = words.pop();
console.log(el2);
console.log(words);

Output

["first", "second", "third", "fourth"]

["second", "third", "fourth"]

["second", "third"]
With the push() method, we add one or more elements at the end of the array.
The shift() method removes the first element from an array and returns the removed element.
The pop() method removes the last element from an array and returns the element.

Check out all 100 + JavaScript Examples
Check out all JavaScript programs at All JavaScript Programs 



Comments