Open In App

Types of Arrays in JavaScript

Last Updated : 22 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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]);


Output

Numeric 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]);


Output

String 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]);


Output

Array 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]);


Output

Array 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]);


Output

Array 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);


Output

Array with empty and other values:
 [ 1, 2, <1 empty item>, 'Geek', <1 empty item>, 3 ]
Filtered Empty Array:  []


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads