JavaScript Fundamentals: Arrays
What is an Array in JavaScript?
An array is a special type of data type that allows you to store multiple values in a single variable.
Arrays are one of the most commonly used data structures in JavaScript. They allow developers to store and manipulate data in a way that is both easy to understand and powerful.
An array is simply a collection of values.
The values in an array can be of any data type, including numbers, strings, objects, and even other arrays. Arrays are created using the array literal syntax, which is simply a set of values enclosed in square brackets:
var myArray = [1, 2, 3];
The above code creates an array with three values: 1, 2, and 3. These values can be accessed using array indices, which are zero-based. In other words, the first element in the array is myArray[0]
, the second element is myArray[1]
, and so on.
Types of Arrays in JavaScript
There are two types of arrays in JavaScript:
- Standard Arrays
- Associative Arrays
A standard array is a numerically indexed array, where each element is accessed by its numerical index. An associative array is an array where each element is accessed by a key.
Here is an example of a standard array in JavaScript:
var colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"];
This is a standard JavaScript array that contains a list of colors.
An associative array is a data structure that stores key-value pairs. In JavaScript, an associative array is simply an object.
Here is an example of an associative array in JavaScript:
var myObj = {
"name": "Aphinya",
"website": "zipply.io",
"city": "Lumsden"
};
JavaScript Array Methods
Arrays are also mutable, which means that they can be changed after they are created. Elements can be added, removed, or modified using the array's built-in methods. Here are some of the most commonly used methods for arrays:
- push(): This method adds one or more elements to the end of an array.
- pop(): This method removes the last element from an array.
- shift(): This method removes the first element from an array.
- unshift(): This method adds one or more elements to the beginning of an array.
- splice(): This method can be used to add or remove elements from an array.
- concat(): This method returns a new array that is the combination of two or more arrays.
- slice(): This method returns a new array that is a subset of an existing array.
- indexOf(): This method returns the first index at which a given element can be found in an array.
- forEach(): This method executes a provided function once for each array element.
Here's an example of how to use array methods:
var myArray = [1, 2, 3];
myArray.push(4); // adds 4 to the end of the array
myArray.unshift(0); // adds 0 to the beginning of the array
myArray.pop(); // removes the last element from the array
myArray.shift(); // removes the first element from the array
myArray.splice(2, 1); // removes the element at index 2
myArray.reverse(); // reverses the order of the elements in the array
myArray.sort(); // sorts the elements in the array
There are many other methods that can be used to manipulate arrays, but the above are some of the most commonly used.
Arrays are an essential part of JavaScript and are used in many different applications. If you're just getting started with JavaScript, make sure to spend some time learning about arrays and how to use them.