add in javascript array code example
Example 1: javascript append to array
var colors=["red","white"];
colors.push("blue");//append 'blue' to colors
Example 2: pushing to an array
array = ["hello"]
array.push("world");
console.log(array);
//output =>
["hello", "world"]
Example 3: javascript array add
// example:
let yourArray = [1, 2, 3];
yourArray.push(4); // yourArray = [1, 2, 3, 4]
// syntax:
// .push();
Example 4: pushing element in array in javascript
array = ["hello"]
array.push("world");
Example 5: js add function to array
//create an new function that can be used by any array
Array.prototype.second = function() {
return this[1];
};
var myArray = ["item1","item2","item3"];
console.log(myArray.second());//returns 'item2'
Example 6: javascript append to array
arr = [1, 2, 3, 4]
arr.push(5) // adds element to end
arr.unshift(0) // adds element to beginning