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.
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.
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
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];
The length property of an array returns the length of an array.
console.log(cars.length);
cars.forEach(function (item, index, array) {
console.log(item, index);
});
var newArray = cars.push("Innova"); var lastArray = cars.pop();
var first = cars.shift();
var newLength = cars.unshift("Alto"); var pos = cars.indexOf("Innova"); var removedItem = cars.splice(pos, 1);
var carsCopy = cars.slice();
typeof cars;
The difference between Arrays and Objects is that arrays use numbered indexes whereas objects use named indexes.
The coronavirus outbreak has drastically changed the way we live our lives. Yes, that's absolutely…
Sales and marketing teams help attract, convert, and retain customers to ensure an organization’s long-term…
Are you an owner of a small business who’s trying to come up with ways…
Introduction When patients bring me their lab reports, the confusion is almost always the same.…
Are you excited about remodeling your house after a long time? Perhaps if you're planning…
The practice of yoga teaches us to be present, patient and mindful of our decisions.…
View Comments
Arrays are a special type of objects. The typeof operator in JavaScript returns object for arrays.