what is javascript array 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: js array
let numbers = [ 11 , 13 , 15 , 17]
for(let i = 0;i<numbers.length;i++) {
console.log(numbers[i])
}
Example 3: javascript array
var food = ["cake", "apple", "Ice-Cream"]
var Number = [Math.floor(Math.random() * food.length)];
console.log(logammounts[woodloot])
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: 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]) ;
}
Example 6: create array with number js
var foo = new Array(45);