javasrcript arrays code example
Example 1: javascript array
var colors = ["red","blue","green"];
for (var i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
Example 2: array javascript
var fruits = ['Apple', 'Banana'];
console.log(fruits.length);
var first = fruits[0];
var last = fruits[fruits.length - 1];
fruits.forEach(function(item, index, array) {
console.log(item, index);
});
var newLength = fruits.push('Orange');
var last = fruits.pop();
var first = fruits.shift();
var newLength = fruits.unshift('Strawberry')
fruits.push('Mango');
var pos = fruits.indexOf('Banana');
var removedItem = fruits.splice(pos, 1);
var vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot'];
console.log(vegetables);
var pos = 1, n = 2;
var removedItems = vegetables.splice(pos, n);
console.log(vegetables);
console.log(removedItems);