2.1 JavaScript Interview Preparation: Arrays

2.1 JavaScript Interview Preparation: Arrays

If you are new to JavaScript, one of the essential concepts to grasp is arrays. An array is a data structure used to store a collection of values or elements under a single variable name. It is an ordered list of values that can be accessed and manipulated using index numbers. In this article, we will explain arrays and traversal in JavaScript for beginners.

What is an Array?

In programming, a variable can store only one value at a time. However, sometimes we need to store multiple values under a single variable. This is where arrays come in handy. An array is a collection of values or elements of the same or different data types that are stored under a single variable name.

An array can be created in JavaScript using the square bracket notation, as shown below:

let arr = [4, 7, 9];

In the above example, we have created an array named arr with three integer values.

Array Traversal

To access the values stored in an array, we need to traverse or loop through the array. There are several ways to traverse an array in JavaScript.

  1. For loop

A for loop is the most common way to traverse an array. We use the for loop to iterate through each element of the array using its index number. Here is an example:

let arr = [4, 7, 9];

for (let i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}

In the above example, we have used a for loop to iterate through the array arr using its index numbers. The arr.length property returns the length of the array, which is 3 in this case.

  1. For in operator

The for in operator is another way to loop through an array. It returns the index number of each element in the array. Here is an example:

let arr = [4, 7, 9];

for (let index in arr) {
  console.log(arr[index]);
}

In the above example, we have used the for in operator to iterate through the array arr. The index variable represents the index number of each element in the array.

  1. For of operator

The for of operator is similar to the for in operator, but it returns the value of each element directly. Here is an example:

let arr = [4, 7, 9];

for (let value of arr) {
  console.log(value);
}

In the above example, we have used the for of operator to iterate through the array arr. The value variable represents the value of each element in the array.

  1. forEach() method

The forEach() method is a built-in method in JavaScript used to loop through an array. It is a method to return a new array like map or filter. In this method, we use a callback function as an argument. Here is an example:

let arr = [4, 7, 9];

arr.forEach(function(value) {
  console.log(value);
});

In the above example, we have used the forEach() method to iterate through the array arr. The callback function function(value) is executed for each element in the array.

Conclusion

In this article, we have explained the concept of arrays and traversal in JavaScript. We have learned how to create an array and how to loop through it using different methods like for loop, for in operator, for of operator, and forEach() method. Understanding arrays and traversal is an essential concept for JavaScript beginners, and mastering it will help you write more efficient and robust code.


The content of this blog post was inspired by the JavaScript - Marathon Interview Questions Series 2023 course on Udemy