DEV Community

Cover image for Arrays in JavaScript
Kavitha
Kavitha

Posted on

Arrays in JavaScript

  • Arrays are one of the most commonly used data structures in JavaScript.
  • They allow us to store multiple values in a single variable, making code cleaner and easier to manage.
  • Instead of creating many variables, arrays help group related values together.

For example, rather than writing:

let mark1 = 85;
let mark2 = 90;
let mark3 = 78;
Enter fullscreen mode Exit fullscreen mode

we can write:

let marks = [85, 90, 78];
Enter fullscreen mode Exit fullscreen mode

This makes the code more organized and scalable.

What is an Array?

An array is a special type of object in JavaScript used to store an ordered collection of values.

let fruits = ["apple", "banana", "orange"];
Enter fullscreen mode Exit fullscreen mode

Here:

  • "apple" is at index 0
  • "banana" is at index 1
  • "orange" is at index 2

JavaScript arrays always start indexing from 0.

Accessing Array Elements

You can access elements using their index.

let fruits = ["apple", "banana", "orange"];

console.log(fruits[0]);
console.log(fruits[1]);
Enter fullscreen mode Exit fullscreen mode

Output:

apple
banana
Enter fullscreen mode Exit fullscreen mode

Updating Values

You can modify any value in the array.

let numbers = [10, 20, 30];

numbers[1] = 50;

console.log(numbers);
Enter fullscreen mode Exit fullscreen mode

Output:

[10, 50, 30]
Enter fullscreen mode Exit fullscreen mode

Array Length

To find the number of elements in an array, use .length.

let colors = ["red", "blue", "green"];

console.log(colors.length);
Enter fullscreen mode Exit fullscreen mode

Output:

3
Enter fullscreen mode Exit fullscreen mode

Looping Through Arrays

Arrays are commonly used with loops.

let nums = [1, 2, 3, 4, 5];

for (let i = 0; i < nums.length; i++) {
    console.log(nums[i]);
}
Enter fullscreen mode Exit fullscreen mode
  • This prints each element one by one.

Important Built-in Array Methods(To be discussed)

push() – Add element at the end

let arr = [1, 2, 3];
arr.push(4);

console.log(arr);
Enter fullscreen mode Exit fullscreen mode

Output:

[1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

pop() – Remove last element

arr.pop();
Enter fullscreen mode Exit fullscreen mode

shift() – Remove first element

arr.shift();
Enter fullscreen mode Exit fullscreen mode

unshift() – Add element at the beginning

arr.unshift(0);
Enter fullscreen mode Exit fullscreen mode

Arrays Can Store Multiple Data Types

JavaScript arrays can hold different types of values.

let mixed = [10, "hello", true, 25.5];
Enter fullscreen mode Exit fullscreen mode
  • This flexibility is one of JavaScript’s strengths.

Is Array a Data Type?

Arrays are technically objects in JavaScript.

typeof [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

Output:

"object"
Enter fullscreen mode Exit fullscreen mode

To specifically check for arrays:

Array.isArray([1, 2, 3]);
Enter fullscreen mode Exit fullscreen mode

Output:

true
Enter fullscreen mode Exit fullscreen mode

Conclusion

  • Arrays are essential in JavaScript programming.
  • They help manage collections of data efficiently and are widely used in web development, data handling, and algorithms.

Top comments (0)