A JavaScript Array stores an ordered collection of values in one variable. Array indexes start at 0, so the first item is at index 0 and the last item is normally at array.length - 1.
const languages = ["JavaScript", "PHP", "Python"];
console.log(languages[0]); // JavaScript
console.log(languages.length); // 3
Use arrays for lists such as names, scores, products, form values, coordinates, or rows of data. Arrays can hold values of any JavaScript type, including objects and other arrays.
The array literal syntax [] is the usual and clearest way to create an array.
const empty = [];
const scripts = ["PHP", "JavaScript", "Python"];
const mixed = [42, "text", true, { name: "Ravi" }];
The Array constructor also exists, but a single numeric argument has special behavior: new Array(3) creates an array with three empty slots, not an array containing the number 3. See creating JavaScript arrays for the full set of creation patterns.
const colors = ["red", "green", "blue"];
console.log(colors[1]); // green
colors[1] = "yellow";
console.log(colors); // ["red", "yellow", "blue"]
Reading an index that does not exist returns undefined. Assigning to an index beyond the current end can create empty slots between the old and new positions.
The length property is one greater than the highest array index. For a normal dense array, that is also the number of elements.
const items = ["A", "B", "C", "D"];
console.log(items.length); // 4
console.log(items[items.length - 1]); // D
Sparse arrays are an important edge case: length can be larger than the number of actually populated elements. The array length tutorial explains reading, changing, truncating, and clearing arrays with length.
| Operation | Method / property | What it does |
|---|---|---|
| Create | Array creation | Create arrays and add initial values. |
| Display / iterate | Display array elements | Read one item or loop through the collection. |
| Size | length | Read or change the array length. |
| Add at end | push() | Mutates the array and returns the new length. |
| Remove from end | pop() | Mutates the array and returns the removed value. |
| Add at start | unshift() | Adds one or more values at index 0. |
| Remove from start | shift() | Removes and returns the first value. |
| Combine | concat() | Returns a new combined array without changing the originals. |
| Join as text | join() | Returns a string using a chosen separator. |
| Convert to text | toString() | Returns comma-separated text. |
| Object value | valueOf() | Returns the array object itself; it is not a display method. |
| Search | indexOf() | Finds the first matching index. |
| Extract | slice() | Returns a selected portion as a new array. |
| Add/remove by position | splice() | Mutates the array at a selected index. |
| Reverse | reverse() | Reverses the array in place. |
| Sort | sort() | Sorts in place; numeric sorting needs a compare function. |
| String to array | String.split() | Splits a string into an array using a separator. |
| Clear | Empty an array | Remove all items while choosing whether references should be preserved. |
| Nested data | Two-dimensional arrays | Use arrays inside arrays for rows and columns. |
Add User Values with push() Add and Remove Values with splice()
Some methods change the existing array. Others return a new value or a new array. This difference matters when the same array is referenced elsewhere.
const original = [1, 2];
const sameArray = original;
original.push(3); // mutates original
console.log(sameArray); // [1, 2, 3]
const combined = original.concat([4, 5]); // new array
console.log(original); // [1, 2, 3]
console.log(combined); // [1, 2, 3, 4, 5]
push(), pop(), shift(), unshift(), splice(), sort(), and reverse() mutate. Methods such as concat() and slice() return new arrays.
Use a classic for loop when you need the numeric index, or for...of when you mainly need each value.
const languages = ["JavaScript", "PHP", "Python"];
for (let i = 0; i < languages.length; i++) {
console.log(i, languages[i]);
}
for (const language of languages) {
console.log(language);
}
Avoid using for...in as the general-purpose array loop because it enumerates property names rather than being designed specifically for array values. See displaying array elements for more iteration and DOM-output patterns.
const values = [10, 20, 30];
console.log(Array.isArray(values)); // true
console.log(Array.isArray({0: 10, length: 1})); // false
Array.isArray() is the reliable built-in test when your code needs to distinguish arrays from ordinary objects.
Spread syntax and slice() can make a shallow copy. Nested objects or nested arrays are still shared references.
const source = [1, 2, 3];
const copy1 = [...source];
const copy2 = source.slice();
console.log(copy1 === source); // false
const matrix = [
[1, 2, 3],
[4, 5, 6]
];
console.log(matrix[1][2]); // 6
JavaScript does not have a special two-dimensional array type; it uses arrays containing other arrays. See the two-dimensional array tutorial for jagged arrays, nested loops, and common initialization mistakes.
1 for the first item instead of index 0.array[array.length] instead of array[array.length - 1].push() or unshift() to return the array; they return the new length.pop() or shift() to return the new array; they return the removed value.new Array(5) when you intended an array containing the number 5.for...in when for...of or an index loop better matches the task.The following Plus2net videos demonstrate creating arrays and displaying array elements.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.