An Array Is an Ordered List
Arrays in JavaScript hold an ordered sequence of values. The values can be any type — numbers, strings, objects, even other arrays — and they don't all have to be the same type. You create one with square brackets:
That literal syntax is how you'll create arrays 99% of the time. There's also new Array(...), but it has a sharp edge: new Array(3) creates an array of length 3 with no actual elements, not [3]. Stick with [].
Indexing Starts at Zero
You access elements by position, starting from 0:
Two things to note. First, indexing off the end of the array doesn't throw — it returns undefined. That's a common source of "why is this undefined?" bugs. Second, you can assign to any index, including one past the end, and JavaScript will happily extend the array:
Positions 3 and 4 become "empty slots" (sparse holes). The length jumps to 6. You rarely want this — it's a side effect of how flexible arrays are, not a feature to lean on.
length Is Live
array.length is always one more than the highest index. It updates automatically when you add or remove elements:
That last trick is worth remembering: assigning to length truncates the array. Setting nums.length = 0 is a quick way to empty it in place.
push, pop, shift, unshift
Four methods for adding and removing from the ends:
push(x)adds to the end, returns the new length.pop()removes from the end, returns the removed element.unshift(x)adds to the start.shift()removes from the start.
push/pop are fast. shift/unshift have to reindex every other element, so they're slower on large arrays — fine for everyday sizes, worth knowing if you're working with millions of items.
slice vs splice
These two have similar names and completely different behavior. Keep them straight and you'll save yourself a lot of head-scratching.
slice(start, end) returns a copy of a portion of the array. The original is untouched. end is exclusive:
splice(start, deleteCount, ...items) mutates the array. It can delete, insert, or both, and it returns whatever it removed:
Rule of thumb: if the word makes you think of slicing a copy off a loaf, that's slice. If it makes you think of splicing film (cutting and inserting), that's splice.
Finding Things: indexOf, includes, find
Three ways to look something up, for different situations:
indexOf(x)gives you the position, or-1if not found. Useful when you need the index.includes(x)gives you a boolean. Cleaner when you just want to know "is this in here?" It also handlesNaNcorrectly, whichindexOfdoesn't.find(predicate)runs a function for each element and returns the first one that matches. This is the one for arrays of objects, whereindexOfandincludesonly do strict equality checks.
Looping Through an Array
A few good ways, each suited to a different situation:
for...of is the default choice — readable, works with any iterable, gives you the value directly. forEach is fine when you also want the index and you're not breaking out of the loop early (you can't break out of forEach). The classic for with an index is verbose but gives you full control — use it when you need to skip elements, iterate backward, or both.
One loop to avoid: for...in. It iterates keys and includes inherited properties. That's for objects, not arrays.
Copying and Combining
Arrays are reference values. Assigning one to another doesn't copy — it makes two names for the same array:
To actually copy, use the spread operator or slice():
Both give you a shallow copy — the top-level array is new, but if the elements are objects, those objects are still shared. Spread is also the cleanest way to combine arrays:
Arrays of Objects
In real code, most arrays hold objects. The same methods work, but the matching gets more interesting:
filter returns a new array of elements that match a predicate. map returns a new array where each element is transformed. Chaining them is the bread and butter of working with arrays of objects — and the next doc page goes deeper on that family of methods.
Arrays vs Objects
A quick mental model: use an array when order matters and you're working with a list of the same kind of thing. Use an object (or a Map) when you're keying things by name. "First, second, third" is array territory. "Look up the user with id 42" is object or Map territory.
You can mix them — arrays of objects are everywhere — but don't use an array as a keyed dictionary, and don't use an object as an ordered list. Each does one job well.
Next: Destructuring
Pulling values out of arrays by index (and properties out of objects by name) comes up often enough that JavaScript has dedicated syntax for it. That's destructuring, and it's up next.
Frequently Asked Questions
How do you create an array in JavaScript?
Use square-bracket literal syntax: const fruits = ['apple', 'banana']. That's the idiomatic way. The Array constructor (new Array(3)) exists but is rarely what you want — new Array(3) creates a length-3 array with no actual elements, which surprises people.
How do I remove an item from an array in JavaScript?
From the end, use pop(). From the start, use shift(). From anywhere in the middle, use splice(index, 1) to mutate in place, or filter(...) to get a new array without the item. splice changes the original; filter doesn't.
What's the difference between slice and splice?
slice(start, end) returns a shallow copy of a portion of the array and leaves the original alone. splice(start, deleteCount, ...items) mutates the original — it removes elements, optionally inserts new ones, and returns the removed ones. One letter, very different behavior.
How do you check if a value is in a JavaScript array?
Use array.includes(value) — it returns true or false and handles NaN correctly. indexOf(value) returns the position or -1 if not found, which is useful when you need the index too. For complex matching, reach for some(predicate) or find(predicate).