Published
An array in JavaScript is a type of variable that can hold multiple values. It acts like a list, allowing you to store different elements (like numbers, strings, other arrays, objects, etc.) in one place. You can access or manipulate those values by referring to the index number, a number that shows their position in the array.
There are various ways to declare (create) an array in JavaScript. The most common ways are to use the array literal syntax ([]), or to create an instance of an array using the new keyword. Elements within an array are separated by commas.
var myArray = []; // This is an empty array
var fruits = ['apple', 'banana', 'cherry']; // This is an array of strings
var differentTypes = [1, 'two', [3, 'four']]; // This array includes different types of elements
var newArray = new Array('a', 'b', 'c'); // This array is created with new keyword.
You can use an element’s index to access it from the array. Array indexes are zero-based, meaning the first item is at index 0, the second at index 1, and so on. You can also change an array's element by using its index number.
var fruits = ['apple', 'banana', 'cherry'];
console.log(fruits[1]); // This will print 'banana'
fruits[0] = 'avocado'; // This will change 'apple' to 'avocado'
Arrays come with various built-in methods that lets you perform operations like adding and removing elements or finding the index of an item.
var fruits = ['apple', 'banana', 'cherry'];
fruits.push('durian'); // This will add 'durian' to the end of the array
var removedFruit = fruits.pop(); // This removes 'durian' from the end of the array
var fruitIndex = fruits.indexOf('banana'); // This gets the index of 'banana'
By reading this article, you've invested 0.89 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.
Imagine you have a big toy box in which you put all your toys. An array is like that toy box, but instead of toys, you can put things like numbers, words, or even other boxes (which are like smaller arrays) inside of it!
When you get a new toy box, it's empty at first. You fill it up by putting your toys in it. In JavaScript, you create an array (the box) and then you can put different things (toys) inside of it.
var myArray = ['toy car', 'teddy bear', 'ball'];
Just like you can pick out a specific toy from your box, you can also pick out specific items from an array. You do this by knowing where they are in the array. For example, 'toy car' is the first item in the array we created, so we can get it like this:
var firstToy = myArray[0];
Just like you can add new toys to your toy box, you can also add new items to an array. Let's say we get a new toy, a 'doll'. We can add it to the end of the array like this:
myArray.push('doll');
Sometimes, you might want to take a toy out of your toy box. In the same way, you can take items out of an array. If we want to remove the 'doll' we just added, we can do it like this:
myArray.pop();
By reading this article, you've invested 1.19 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.
If you want to learn more about JavaScript arrays and other JavaScript topics, check out our detailed explanation on JavaScript Data Types and Variables
About author
Roman Y.
Senior Software Engineer at Nike
Certain articles only distributed to subscribers through email