Types of Arrays in JavaScript
Last Updated :
22 Jan, 2024
A JavaScript array is a collection of multiple values at different memory blocks but with the same name. The values stored in an array can be accessed by specifying the indexes inside the square brackets starting from 0 and going to the array length - 1([0]...[n-1]).
A JavaScript array can be classified into multiple types:
Numeric Array
A numeric array is an array that contains only the numeric values as elements that are stored at different memory locations but in a consecutive manner.
Example: The below code example is an implementation of defining and using a numeric array.
JavaScript
const numArr = [1, 2, 3, 4, 5];
console.log("Numeric Array: ", numArr);
console.log("First element: ", numArr[0]);
OutputNumeric Array: [ 1, 2, 3, 4, 5 ]
First element: 1
String Array
A string array contains only the string elements which means it has all the memory blocks filled with the string values that can be accessed using the indexing syntax.
Example: The below code explains the string array implementation.
JavaScript
const strArr =
['GeeksforGeeks', 'JavaScript', 'TypeScript'];
console.log("String Array: ", strArr);
console.log("First element: ", strArr[0]);
OutputString Array: [ 'GeeksforGeeks', 'JavaScript', 'TypeScript' ]
First element: GeeksforGeeks
Array of Arrays (2D Arrays)
An array of arrays or the 2D array contains the multiple arrays as its elements that are stored at different memory locations. It can be used to create the nested arrays.
Example: The below example is the practical implemntation of the 2D arrays in JavaScript.
JavaScript
const arrOfArr =
[
[1, 2, 3],
['GeeksforGeeks', 'JavaScript']
];
console.log("Array of Arrays: ", arrOfArr);
console.log("First Array: ", arrOfArr[0]);
console.log("Second Array: ", arrOfArr[1]);
OutputArray of Arrays: [ [ 1, 2, 3 ], [ 'GeeksforGeeks', 'JavaScript' ] ]
First Array: [ 1, 2, 3 ]
Second Array: [ 'GeeksforGeeks', 'JavaScript' ]
Array of Objects
An array of objects is a array that contains the JavaScript Objects with different properties as elements stored at the different memory locations.
Example: The below code implements the Array of Objects in JavaScript.
JavaScript
const objectsArray =
[
{
type: "Company",
name: "GeeksforGeeks"
},
{
type: "Cricketer",
name: "Virat Kohli"
}
];
console.log("Array of Objects: ", objectsArray);
console.log("First Object: ", objectsArray[0]);
console.log("Second Object: ", objectsArray[1]);
OutputArray of Objects: [
{ type: 'Company', name: 'GeeksforGeeks' },
{ type: 'Cricketer', name: 'Virat Kohli' }
]
First Object: { type: 'Company', name: 'GeeksforGeeks' }
Second Object: { type: 'Cri...
Mixed Arrays
The mixed arrays in JavaScript can contain elements of multiple data types stored at contiguous memory location with same name that can be accessed using the indexing syntax.
Example: The below code is the practical implementation of the mixed arrays in JavaScript.
JavaScript
const mixedArr =
[
[1, 2, 3],
"GeeksforGeeks",
23,
{
cricketer: "Virat Kohli"
}
];
console.log("Array element: ", mixedArr[0]);
console.log("String element: ", mixedArr[1]);
console.log("Number element: ", mixedArr[2]);
console.log("Object element: ", mixedArr[3]);
OutputArray element: [ 1, 2, 3 ]
String element: GeeksforGeeks
Number element: 23
Object element: { cricketer: 'Virat Kohli' }
Array with empty values
An array with empty values can contain the empty or blank values as the elements of the array.
Example: The below code explains the empty values array in JavaScript.
JavaScript
const emptyValArray = [1, 2, , "Geek", , 3];
console.log
("Array with empty and other values:\n", emptyValArray);
const emptyArray =
emptyValArray.filter(val => val === undefined);
console.log("Filtered Empty Array: ", emptyArray);
OutputArray with empty and other values:
[ 1, 2, <1 empty item>, 'Geek', <1 empty item>, 3 ]
Filtered Empty Array: []
Similar Reads
JavaScript Data Types In JavaScript, each value has a data type, defining its nature (e.g., Number, String, Boolean) and operations. Data types are categorized into Primitive (e.g., String, Number) and Non-Primitive (e.g., Objects, Arrays).Primitive Data Type1. NumberThe Number data type in JavaScript includes both integ
5 min read
JavaScript Arrays In JavaScript, an array is an ordered list of values. Each value is called an element, and each element has a numeric position in the array, known as its index. Arrays in JavaScript are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.Array in JavaScriptWhy Use
7 min read
JavaScript Array push() Method The push() method in JavaScript adds one or more elements to the end of an array and returns the new length of the array. It modifies the original array, increasing its length by the number of elements added.Syntaxarr.push(element0, element1, ⦠, elementN);ParametersThis method contains as many numb
3 min read
Are JavaScript arrays objects? Yes, JavaScript arrays are actually specialized objects, with indexed keys and special properties. They have a length property and are technically instances of the Array constructor.JavaScriptconst a = [10, 20, 30]; console.log(typeof a);Outputobject You can add non-integer properties to arrays, mak
2 min read
Dense and Sparse Array in JavaScript In JavaScript, an array is a data structure used for storing a collection of values. Each value in the collection is assigned a unique index number. Arrays in JavaScript can contain values of any data type, such as strings, numbers, objects, and even other arrays. In JavaScript, arrays can be of two
7 min read
JavaScript Array of() Method The Javascript array.of() method is an inbuilt method in JavaScript that creates a new array instance with variables present as the argument of the method.Syntax:Array.of(element0, element1, ....)Parameters: Parameters present are element0, element1, .... which are basically an element for which the
2 min read
JavaScript Array Reference JavaScript Array is used to store multiple elements in a single variable. It can hold various data types, including numbers, strings, objects, and even other arrays. It is often used when we want to store a list of elements and access them by a single variable.Syntax:const arr = ["Item1", "Item2", "
4 min read
JavaScript 2D Arrays Generally, in JavaScript, we deal with one-dimensional or linear arrays that contain multiple elements of the same type stored at different memory locations with the same name. But, there are also 2D array concepts that exist in JavaScript. What are 2D Arrays?A 2D array is commonly known as an array
3 min read
JavaScript Array toString() Method The toString() method returns a string with array values separated by commas. The toString() method does not change the original array. This method converts an array into a readable string format, often for display or logging purposes.Syntax:arr.toString()Parameters: This method does not accept any
3 min read
Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J
3 min read