In Javascript, an Array is used to store multiple values in a single variable. It is a data structure that stores a collection of values with different data types, such as numbers, strings, or objects. The array elements are indexed starting from 0, and they can be accessed via these indexes using various methods in Javascript.
Unlike C or C++, where an array is a variable or a data type, an array is an object in Javascript.
Objects in JavaScript are described as an unordered collection of connected data, of primitive or reference types, in the form of "key: value" pairs. In the context of an object, these keys can be variables or functions, referred to as properties and methods, respectively.
You can store keyed collections of values in objects. That's all right. However, we often require an ordered collection, with first, second, third elements, and so on.
For example, we might want to use it to keep track of a list of users, employees, faculty, and so on.
It is not practical to utilise an object since it lacks methods for managing element order. We can't add a new property "between" the ones that already exist. Objects aren't designed to be used in this way.
To hold ordered collections, there is a particular data structure called Array.
There are three best ways to construct array in JS(JavaScript)
Creating an array using array literal
Creating instance of Array directly (using new keyword)
Using an Array constructor (using new keyword)
Creating an array using array literal
In JavaScript, we can create an array using an array literal, which is a list of values enclosed in square brackets []. The values enclosed within the brackets are elements of an array which can hold any data type.
Syntax
let arrayName = [element1, element2, ...];
You can also try this code with Online Javascript Compiler
element1, element1: These are the elements of our array.
Example
JavaScript
JavaScript
// Initializing first array
let names = ["Smith", "Bob", "Mark", "Luke"];
console.log(names)
//Initializing second array
let marks = [70, 81, 93, 78];
console.log(marks)
You can also try this code with Online Javascript Compiler
In the above example, we initialized and displayed two array names and marks using array literals. The first array holds values of string types, while the other has values of type numbers.
Creating instance of Array directly (using new keyword)
In JavaScript, we can create an instance of an array directly using the new keyword followed by the Array constructor. The Array constructor is an in-built function using which we can create new arrays in JavaScript.
Syntax
let arrayInstance = new Array();
You can also try this code with Online Javascript Compiler
In the above example, we created an instance of an array using the new keyword and the Array constructor, which will create an empty array for us. After that, we explicitly passed the array elements and printed them using the console.log() method.
Using an Array constructor (using new keyword)
In this method, we don't need to pass the array elements explicitly. Instead, we can pass them as arguments in the Array constructor itself.
Syntax
let arrayName = new Array(element1, element2, ...);
You can also try this code with Online Javascript Compiler
new Array(): The new keyword is used to create a new instance of the Array constructor.
element1, element2: These are the elements of our array which are passed during array initialization.
Example
JavaScript
JavaScript
// Creating an array of number
let numbers = new Array(1, 2, 3, 4, 5);
console.log(numbers);
console.log();
// Creating an array of strings
let names = new Array("Smith", 'Bob', 'David');
console.log(names);
You can also try this code with Online Javascript Compiler
In JavaScript, an array is a fundamental data structure. An array is a special type of object that can store multiple values under a single variable. Unlike other languages, arrays in JavaScript can have values of different data types, such as numbers, strings, and objects.
How many types of arrays are there in JS?
In Javascript, arrays are divided into two types, one-dimensional arrays and multi-dimensional arrays. The one-dimensional or linear array consists of only one row or column. Meanwhile, a multi-dimensional array consists of one or more nested arrays.
What are two forms of array?
In Javascript, there are two forms of arrays literal arrays and constructed arrays. The literal arrays are created using the array literal enclosed within square brackets []. The constructed arrays are created using the new keyword and the Array constructor function.
Conclusion
In this article, we discussed the topic of Arrays in JS. We started by introducing arrays in general. Later on, we discussed declarations, functionalities, methods, loops, 2D arrays and what not to do in arrays.
We hope this blog has helped you enhance your knowledge of Arrays in JS. If you want to learn more, then check out our articles.