Indexed Data Structures in JavaScript I
JavaScript·4 min read·Jan 1, 2025
In JavaScript, an array is a dynamic structure that contains a collection of data organized into an ordered list or sequence.
Arrays are often used to store related elements of the same type — but not necessarily — like strings, numbers, booleans, arrays, objects, and so on.
They allow developers to create complex structures such as stacks, queues, and matrices, that can then be used in sorting, searching, and filtering algorithms.
Creating new array instances
To create a new empty array instance, you can use the square brackets syntax []:
array = [];On the other hand, to create a non-empty array instance, you can separate each of its elements by a comma character as follows:
array = [value1, value2, ..., valueN];Example
In this example, we're declaring and initializing a new variable named students with an array containing two strings:
let students = ["John Doe", "Jack Morgan"];Accessing array elements
Each element of an array is associated with a unique numerical key called an index, which represents its position within the array.
The 1st element of the array is located at index 0, the 2nd element at index 1, and so on.
To access the value stored at a specific index, you can use the following syntax:
array[index]Unlock the Build CLI Apps in JavaScript & Node.js module
Learn how to build, integrate, and safeguard scalable CLI tools, scripts, and apps with JavaScript, Node.js, npm, and Git.
You get immediate access to:
- 45 focused lessons across JavaScript, Node.js, npm, and Git
- 17 real-world projects with commented solutions
- Ongoing updates to this bundle
- Lifetime access to this bundle