In today's lesson, we'll explore arrays in JavaScript, a versatile and fundamental data structure. An array is an ordered collection of elements that can be of mixed data types. Arrays in JavaScript are mutable, meaning their elements can be changed after creation. However, arrays can be made immutable using certain techniques if needed.
The beauty of arrays lies in their simplicity and efficiency; they allow for easy storage, access, and manipulation of data. By the end of this lesson, you'll be able to create, manipulate, and understand the unique applications of arrays in JavaScript.
An array in JavaScript is an ordered collection of elements, which can be of any type, including numbers, strings, objects, and even other arrays.
Consider this JavaScript array declaration as an example:
JavaScript1class ArrayExample { 2 createMixedArray() { 3 return ["apple", 42, true, { name: "banana" }, [1, 2, 3]]; 4 } 5} 6 7// Create an instance and call the `createMixedArray` method 8const arrayExample = new ArrayExample(); 9console.log(arrayExample.createMixedArray()); 10// Output: ['apple', 42, true, { name: 'banana' }, [1, 2, 3]]
Arrays are created by enclosing elements in square brackets []
, with commas to separate them — this is known as the array literal syntax. You can also create arrays using the Array
constructor function. It is considered a best practice to declare arrays with const
in JavaScript because const
ensures that the variable holding the array reference cannot be reassigned to a different array or any other value. However, while the reference to the array is immutable, the contents of the array itself are still mutable (i.e., you can change the elements of the array).
In the next JavaScript example, we illustrate array creation:
JavaScript1class ArrayExample { 2 createArrays() { 3 const arrayLiteral = ["apple", "banana", "cherry"]; 4 const fromConstructor = new Array("apple", "banana", "cherry"); 5 return { arrayLiteral, fromConstructor }; 6 } 7} 8 9// Create an instance and call the `createArrays` method 10const arrayExample = new ArrayExample(); 11console.log(arrayExample.createArrays()); 12// Output: { arrayLiteral: ['apple', 'banana', 'cherry'], fromConstructor: ['apple', 'banana', 'cherry'] }
This example creates arrays via literals and constructors.
Just like strings, array elements are accessible via indexes. Indexes in JavaScript arrays are zero-based, meaning the first element has an index of 0, the second element has an index of 1, and so on. Array indexing in JavaScript allows you to access, modify, and manipulate the elements stored within an array using their respective positions.
The length
property of an array returns the number of elements present in the array and can be used to easily determine its size or iterate over the array. Though arrays are mutable by default, you can create new arrays from existing ones or use Object.freeze()
to enforce immutability.
Consider the following example that depicts inspecting, modifying arrays, and demonstrating the length
property:
JavaScript1class ArrayExample { 2 inspectArray() { 3 const myArray = ["apple", "banana", "cherry", "durian", "elderberry"]; 4 console.log(myArray[1]); // Output: 'banana' 5 console.log(myArray[myArray.length - 1]); // Output: 'elderberry' 6 console.log(myArray.slice(2, 4)); // Output: ['cherry', 'durian'] 7 8 const newArray = [...myArray.slice(1, 3), "dragonfruit"]; 9 console.log(newArray); // Output: ['banana', 'cherry', 'dragonfruit'] 10 11 Object.freeze(newArray); 12 13 // Attempt to modify the frozen array 14 try { 15 newArray[0] = "blueberry"; 16 } catch (error) { 17 console.log("Error: Cannot modify a frozen array"); // Output: Error message 18 } 19 console.log(newArray); // Output: ['banana', 'cherry', 'dragonfruit'] 20 } 21} 22 23// Create an instance and call the `inspectArray` method 24const arrayExample = new ArrayExample(); 25arrayExample.inspectArray();
Here's a deeper dive into some of the array methods used:
slice(start, end)
returns a new array containing the elements from the start index up to but not including the end index.length
is a property that gives the total number of elements in the array.Object.freeze(array)
makes the array immutable, meaning any changes to it will throw an error.
JavaScript arrays allow a range of operations. The concat()
method concatenates two arrays, while loops or the Array
fill method can be used to repeat elements of an array. The includes()
method checks if a particular item is present in the array. Additionally, the map()
method creates a new array populated with the results of calling a provided function on every element in the array.
Here's an example demonstrating these operations:
JavaScript1class ArrayExample { 2 arrayOperations() { 3 const array1 = ["apple", "banana"]; 4 const array2 = ["cherry", "durian"]; 5 const array3 = array1.concat(array2); 6 console.log(array3); // Output: ['apple', 'banana', 'cherry', 'durian'] 7 8 const array4 = Array(3).fill(array1).flat(); 9 console.log(array4); // Output: ['apple', 'banana', 'apple', 'banana', 'apple', 'banana'] 10 11 console.log(array1.includes("apple")); // Output: true 12 13 const mappedArray = array1.map(fruit => `${fruit} pie`); 14 console.log(mappedArray); // Output: ['apple pie', 'banana pie'] 15 } 16} 17 18// Create an instance and call the `arrayOperations` method 19const arrayExample = new ArrayExample(); 20arrayExample.arrayOperations();
Here’s a breakdown of the array methods used:
concat(anotherArray)
returns a new array that is the result of joining two or more arrays.Array(n).fill(value)
creates an array withn
copies of thevalue
.flat()
is used to flatten nested arrays into a single array.includes(element)
checks if the array includes a certain element and returnstrue
orfalse
.map(callback)
creates a new array populated with the results of calling a provided function on every element in the array. In this case, we use an arrow functionfruit => `${fruit} pie`
for concise syntax. Arrow functions provide a shorter way to write functions and are especially useful for inline callbacks.
An array can contain another array, resulting in a nested array. In a nested array, you can access elements by chaining indices. The first index accesses an element within the top-level array, and the subsequent index(es) access elements within the sub-array. Here's an example of creating and indexing a nested array:
JavaScript1class ArrayExample { 2 createNestedArray() { 3 return ["apple", ["banana", "cherry"]]; 4 } 5} 6 7// Create an instance and call the `createNestedArray` method 8const arrayExample = new ArrayExample(); 9const nestedArray = arrayExample.createNestedArray(); 10console.log(nestedArray); // Output: ['apple', ['banana', 'cherry']] 11 12// Access elements in the nested array 13console.log(nestedArray[0]); // Output: 'apple' - top-level array element 14console.log(nestedArray[1]); // Output: ['banana', 'cherry'] - sub-array 15console.log(nestedArray[1][0]); // Output: 'banana' - element within the sub-array 16console.log(nestedArray[1][1]); // Output: 'cherry' - another element within the sub-array
Array destructuring allows you to extract values from an array and assign them to individual variables in a single statement. This helps in breaking down complex, nested data structures into more manageable parts for easier access and manipulation.
JavaScript1// Destructuring an array for easier use 2const [element1, [element2, element3]] = arrayExample.createNestedArray(); 3console.log(element1); // Output: 'apple' 4console.log(element2); // Output: 'banana' 5console.log(element3); // Output: 'cherry'
You can also create complex nested arrays where each element can be an object containing arrays. Consider the following example with car manufacturers and their models:
JavaScript1class ArrayExample { 2 createNestedObjectArray() { 3 return [ 4 {name: "Ford", models: ["Fiesta", "Focus", "Mustang"]}, 5 {name: "BMW", models: ["320", "X3", "X5"]}, 6 {name: "Fiat", models: ["500", "Panda"]} 7 ]; 8 } 9} 10 11// Create an instance and call the `createNestedObjectArray` method 12const arrayExample = new ArrayExample(); 13const cars = arrayExample.createNestedObjectArray(); 14console.log(cars); 15// Output: [ 16// {name: "Ford", models: ["Fiesta", "Focus", "Mustang"]}, 17// {name: "BMW", models: ["320", "X3", "X5"]}, 18// {name: "Fiat", models: ["500", "Panda"]} 19// ] 20 21// Access elements in the nested object array 22console.log(cars[0].models); // Output: ['Fiesta', 'Focus', 'Mustang'] 23console.log(cars[1].name); // Output: 'BMW' 24console.log(cars[2].models[1]); // Output: 'Panda'
This demonstrates how nested arrays can be used in complex structures, making it easier to organize and access data.
Excellent job! In this lesson, you've learned what arrays are and how to create, inspect, and operate on them. You've also learned some advanced concepts like nested arrays and destructuring.
Going forward, our focus will be on meticulously designed practice exercises that solidify your understanding. Remember, the key to successful learning is practice. Reinvent these examples by breaking and fixing them again. Let's dive deeper!