javascritp new array code example

Example 1: how i create array in javascript

var fruits = ['Apple', 'Banana'];

console.log(fruits.length);
// 2

Example 2: new array from javascript

// Sequence generator function (commonly referred to as "range", e.g. Clojure, PHP etc)
const range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step));

// Generate numbers range 0..4
range(0, 4, 1);
// [0, 1, 2, 3, 4]

// Generate numbers range 1..10 with step of 2
range(1, 10, 2);
// [1, 3, 5, 7, 9]

// Generate the alphabet using Array.from making use of it being ordered as a sequence
range('A'.charCodeAt(0), 'Z'.charCodeAt(0), 1).map(x => String.fromCharCode(x));
// ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

Example 3: array in js

function Array() { return []; }

alert(Array(1, 2, 3)); // An empty alert box

Example 4: how to get create an array in javascript

let names = ['Michael', 'Adam', 'NetNinja'];
//now you are free to use the array
conosle.log(names[0]);

Tags:

Java Example