JavaScript Arrays

JavaScript Tutorial

JavaScript Arrays

In JavaScript Arrays is a special variable that can store multiple values in a single variable. Values inside an array are stored in a list format and separated by commas.

 Creating an Array

1. Using Square Brackets (Most common)

2. Using new Array() (Less common)

Accessing Array Elements

Array index starts from 0.

 Changing Array Elements

 Array Properties

PropertyExampleResult
lengthfruits.lengthNumber of items in array

 

Common Array Methods

MethodAction
push()Add item to end
pop()Remove last item
unshift()Add item to start
shift()Remove first item
sort()Sort values
reverse()Reverse order

Example:

Looping Through an Array

for loop:

forEach() method:

Array of Objects


 

Checking if Value is an Array

Summary

FeatureDescription
Store multiple valuesIn a single variable
Zero-based indexFirst item = index 0
Built-in methodspush, pop, shift, sort, etc.
Easy loopingfor or forEach

Arrays are one of the most important parts of JavaScript and widely used in web development, especially in frameworks like React, Angular, and Vue.

You may also like...