DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
Javascript

JavaScript Arrays

Share

The JavaScript Arrays object is a global object that is used to store multiple values in a single variable.

The JavaScript Arrays object is a global object that is used to store multiple values in a single variable i.e. collection of elements.

Syntax:

var cars = [
"Fiat",
"BMW",
"Nissan"
];

Note: Never put a comma after the last element (like “Nissan”,). This can be effected is inconsistent across different browsers.

Using keyword new:

var cars = new Array("Fiat", "BMW", "Nissan");

Use [] instead because, there is no need to use the JavaScript’s new Array().

Read Also: Detect Browser in JavaScript

How to access:

By index number, you can an array element.

var firstCar = cars[0];

cars[0] is the first element
cars[1] is the second element
cars[2] is the third element

So, for last element:

var lastCar = cars[cars.length - 1];

Length of an Array:

The length property of an array returns the length of an array.

console.log(cars.length);

Looping an Array:

cars.forEach(function (item, index, array) {
console.log(item, index);
});

Add to the end:

var newArray = cars.push("Innova");

Remove from the end:

var lastArray = cars.pop();

Remove from the front:

var first = cars.shift();

Add to the front:

var newLength = cars.unshift("Alto");

Find the index:

var pos = cars.indexOf("Innova");

Remove an item by Index Position:

var removedItem = cars.splice(pos, 1);

Copy an Array:

var carsCopy = cars.slice();

Recognize an Array:

typeof cars;

The difference between Arrays and Objects is that arrays use numbered indexes whereas objects use named indexes.

Jacob Frazier

Based on United States, Jacob Frazier is a skilled JavaScript developer with over 8 years of experience. He is passionate about change and trying new things, both professionally and personally. He loves startups and is extremely proactive.

View Comments

  • Arrays are a special type of objects. The typeof operator in JavaScript returns object for arrays.

Recent Posts

Your Complete Guide to the 461 Visa: Building a Life in Australia with Your New Zealand Partner

People usually stumble onto the 461 visa when they realise there’s no simple way to…

16 hours ago

Why Roarbank Is Transforming Everyday Banking in India

In today’s digital era, people look for convenience, transparency, and genuine rewards from their financial…

20 hours ago

Quality with Quantity: Importing LED Lights Can Save You Big Bucks

If you are planning to start a lighting business and looking for a supplier of…

2 days ago

11 Reasons Why Avoiding Plagiarism is Necessary for SEO and content marketing

If you are a copywriter or a content marketer, you will second my statement that…

4 days ago

6 Common Injuries After a Motorcycle Accident

Motorcycle accidents often lead to severe injuries because riders have minimal protection compared to drivers…

1 week ago

How Automation And SEO Can Improve Customer Experience

No business owner starts his or her business with the hope of making losses. Everyone…

1 week ago