array() in javascript 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: how to create array in javascript
let array_1 = new Array(2);
arr.push("James");
arr.push("Fred");
let array_2 = ["James", "Fred"];
Example 3: js array
var colors = [ "red", "orange", "yellow", "green", "blue" ];
console.log(colors);
console.log(colors[0]);
console.log(colors[1]);
console.log(colors[4]);
colors[4] = "dark blue"
console.log(colors[4]);
Example 4: 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);
Example 5: js arrays
const colors = ["red", "orange", "yellow"];
colors[0];
colors.length;
Example 6: array javascript
special_dates = [];
special_dates.push(new Date('2021/02/12').getTime());
special_dates.push(new Date('2021/02/13').getTime());
special_dates.push(new Date('2021/02/14').getTime());
for (var i = 0; i < special_dates.length; i++) {
console.log(special_dates[i]) ;
}