A JavaScript array is an ordered collection of values. Use square brackets to create one, a zero-based index to read an item, and the length property to find how many positions it has. Methods such as push(), pop(), map(), and filter() let you change or work with its contents.
What is an array in JavaScript?
An array stores values in a specific order, with each value accessible by its position, called an index. For example, an array can hold a list of colors or numbers. JavaScript arrays are indexed collections with a length property, as described in the MDN guide to indexed collections.
As an Amazon Associate I earn from qualifying purchases.
How do I create an array in JavaScript?
Write the values between square brackets and separate them with commas:
Recommended Free Tools
const fruits = ["apple", "banana", "cherry"];
This creates an array with three strings. You can also create an empty array and add values later:
#1 Best Overall
const items = [];
For more examples and details, see the MDN Array reference.
How do I get the first item in an array?
Array indexes start at zero, so use array[0] for the first item, not array[1]:
Rank #2
const fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // "apple"
The second item is at index 1, the third at index 2, and so on. For a non-empty, dense array, the last item is at index length - 1. An index equal to length is beyond the final position.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →How do I find the length of an array?
Read the array’s length property:
const fruits = ["apple", "banana", "cherry"];
console.log(fruits.length); // 3
console.log(fruits[fruits.length - 1]); // "cherry"
length gives the number of array positions counted by the array, while the last-item expression above assumes the array is non-empty and dense. The ECMAScript 2025 specification defines arrays in terms of indexed properties and a length property.
How do I add or remove an item from an array?
For operations at the end of an array, push() adds values and pop() removes one. Both change the original array.
Append with push()
const fruits = ["apple", "banana"];
const newLength = fruits.push("cherry");
console.log(fruits); // ["apple", "banana", "cherry"]
console.log(newLength); // 3
push() returns the new length, not the value it added. The ECMAScript 2025 specification, section 23.1.3.23, defines it as appending its arguments to the array and returning the new length.
Rank #4
Remove the last item with pop()
const fruits = ["apple", "banana", "cherry"];
const removedFruit = fruits.pop();
console.log(removedFruit); // "cherry"
console.log(fruits); // ["apple", "banana"]
pop() returns the removed value and leaves the original array shorter.
Free tools Windows power users keep installed
One-click scans. No signup required.
What is the difference between map() and filter()?
Use map() when you want a result for every existing item; use filter() when you want to keep only items that pass a condition. Both return a new array rather than replacing the original.
Best Value
Transform every item with map()
const numbers = [1, 2, 3];
const doubled = numbers.map((number) => number * 2);
console.log(doubled); // [2, 4, 6]
console.log(numbers); // [1, 2, 3]
Keep matching items with filter()
const numbers = [1, 2, 3];
const largeNumbers = numbers.filter((number) => number > 1);
console.log(largeNumbers); // [2, 3]
console.log(numbers); // [1, 2, 3]
A useful choice is: “Am I changing each item into a corresponding result?” If yes, use map(). “Am I selecting which items to keep?” If yes, use filter(). MDN’s Array reference documents these common array operations.
Do all array methods change the original array?
No. Check the behavior of the specific method instead of assuming all methods mutate the array or all preserve it. For example, push() and pop() change their array, while map() and filter() produce new arrays. slice() returns a portion in a new array; sort() changes the original array. These distinctions are documented method by method in the ECMAScript 2025 specification.
For numeric sorting, provide a comparator because the default sort does not order numbers numerically:
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Quick Recap
const values = [10, 2, 30];
values.sort((a, b) => a - b);
console.log(values); // [2, 10, 30]
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




