removing array elements in JavaScript

JavaScript array elements can be removed in multiple ways using methods pop, shift, splice, filter.

Array.shift()
Shift is a method which knocks off the first most element of an array and returns it! ie
var number = [0, 1, 2, 3, 4, 5]
number.shift() // returns 0
console.log(number) // [1, 2, 3, 4, 5]

Array.pop()
this method removes the last element off an array and returns the removed element ie
var number = [0, 1, 2, 3, 4, 5]
number.pop() // returns 5
console.log(number) // [0, 1, 2, 3, 4]

Array.splice(start, elements)
start is the index at which removing  starts elements is an integer the number of elements to remove it returns spliced elements
var number = [0, 1, 2, 3, 4, 5]
number.splice(0, 1) // returns 0
start can also be a reference ie of an object.

Comments

Popular posts from this blog

How to set content disposition header for nginx server to force content download

Animated daylight on HTML5 Canvas

How to make a static http server in nodejs using express

How to make a Snake Game using JavaScript and html5

JS13K Preparation