create new array js code example
Example 1: javascript array
//create an array like so:
var colors = ["red","blue","green"];
//you can loop through an array like this:
for (var i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
Example 2: how to create an array in node js
// Don't need to provide elements directly, but you can
// FIRST OPTION
var myArray = new Array(/*elements1, elements2*/);
// SECOND OPTION
var mySecondArray = [/*element1, element2*/];
Example 3: how to make an array in javascript
var names = ["Sanjith", "Pranav", "Aadya", "Saharsh"]
Example 4: how to create an array in javascript
let fruits = ['Apple', 'Banana']
console.log(fruits.length)
// 2
Example 5: how to create array in javascript
/*
Array class is a global object that is used
in the construction of arrays
See - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
*/
let array_1 = new Array(2);
arr.push("James");
arr.push("Fred");
let array_2 = ["James", "Fred"];
Example 6: array from js
//Array.from() lets you create Arrays from array-like objects
//(objects with a length property and indexed elements);
//and also:
//More clearly, Array.from(obj, mapFn, thisArg)
//has the same result as Array.from(obj).map(mapFn, thisArg),
//except that it does not create an intermediate array.
//Basically, it's a declaration that overrides the length property of the method
//(so that it has to be used with the same name length),
//setting it with the same value of the given variable.
//The values are still undefined, it's just a different notation. Take a look:
console.log(Array.from(length, (_,i) => i));
// It doesn't works with non-iterables
// In this case we are passing an integer
console.log(Array.from({LENGTH}, (_,i) => i));
// It doesn't work with a property name different from "length"
console.log(Array.from({length}, (_,i) => i));
// It works because overrides the .length property of the array
// The method Array.from() assumes that the property...
// ...is referring to an iterable (also if not declared)
console.log(Array.from(Array(length), (_,i) => i));
// This is the demonstration of the above assertion
// In this case we are using a declared array through...
// ...an instance of the straight method Array()...
// ...that accepts an integer as value
//in case any one reads this a got this from er0s in edabit