TechMentorAI

array

in JavaScript

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.

Imagine array in JavaScript

Array Declaration

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.

Array Indexing

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'

Array Methods

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'

No Time to Read? Learn on the Go!

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!

Imagine array in JavaScript

Creating an Array

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

Using an Array

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

Adding to an Array

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

Removing from an Array

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

No Time to Read? Learn on the Go!

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

Why did I decide to launch this website? Drawing from my rich background in interviewing candidates for a variety of developer roles, I've observed a common pattern: many applicants share similar deficiencies in their knowledge during technical interviews. Motivated by this insight, I established this website with the aim of assisting developers in securing their ideal job. Through straightforward and concise articles, my goal is to not only deepen your understanding of programming language nuances but also to equip you with the insights needed to deliver the precise answers interviewers expect. Essentially, you'll be providing the correct response. I encourage you to spread the word about this site on social media platforms. Echoing the wisdom of an Armenian saying: "Do good and cast it into the water."

EXCLUSIVELY

Certain articles only distributed to subscribers through email