How to usenew keyword in Javascript
The new Array() constructor initializes the array and returns a reference to the array object. It can be used to create arrays of different data types like numbers, strings, nested arrays etc.
Syntax:
const arrayName = new Array(element0, element1, ..., elementN);
Example: This example shows that we can create an array using new keyword also.
Javascript
// Number array const numbers = new Array(56, 74, 32, 45); // String array const courses = new Array( "Javascript" , "Python" , "Java" ); // Empty array const empty = new Array(); // Multidimensional array const matrix = new Array([ [0, 1], [2, 3], ]); console.log(numbers); console.log(courses); console.log(empty); console.log(matrix); |
Output
[ 56, 74, 32, 45 ] [ 'Javascript', 'Python', 'Java' ] [] [ [ [ 0, 1 ], [ 2, 3 ] ] ]
How to Declare an Array in JavaScript ?
In Javascript, Arrays can contain any type of data, numbers, strings, booleans, objects, etc. But typically all elements are of the same type.
Arrays can be single-dimensional with one level of elements, or multi-dimensional with nested arrays.
Contact Us