Array shift()
JavaScript Array shift() Definition
The shift () method removes the first element of the array. Returns the removed item. This method changes the length of the array. If the length of the array is zero, it returns the value of undefined.
Syntax:
array.shift()
Let’s understand the logic with a small example:
let names = ["john", "rick", "bob"];
console.log(names);
let remove = names.shift();
console.log("remove: " + remove)
console.log("new array: "+names)
[“john”, “rick”, “bob”]
remove: john
[“rick”, “bob”]
JavaScript Array shift() Example
let list = [];
let newList = list.shift();
console.log(newList);
console.log(list);
let pushList = list.push("a", "b", "c");
console.log("number of elements: " + pushList)
console.log("new array: " + list)
output:
undefined
[]
number of elements: 3
new array: a,b,c
Depending on this example I am writing this:
let arr = list.shift();
console.log(list)
output:
[“b”, “c”]
Browser Support
Chrome | 1 |
Edge | yes |
Firefox | 1 |
Internet Explorer | 5.5 |
Opera | yes |
Safari | yes |
Android webview | yes |
Chrome for Android | yes |
Edge mobile | yes |
Firefox for Android | 4 |
Opera Android | yes |
Advertisements
2 Comments »