How do you map an array in JavaScript?
Array.prototype.map() runs a callback on every element and returns a new array without changing the original. Learn syntax, object mapping, async use and pitfalls.
466 posts about JavaScript: questions with answers, tutorials and articles, newest first.
Array.prototype.map() runs a callback on every element and returns a new array without changing the original. Learn syntax, object mapping, async use and pitfalls.
Array.prototype.filter() returns a new array with the elements that pass a test. See examples for objects, duplicates, chaining and async pitfalls.
Use reverse() to mutate an array or toReversed() (ES2023) for a new one. Includes string reversal, copies for older runtimes and common pitfalls.
Learn why JavaScript sort() orders numbers as strings, how to write comparators for numbers, text and objects, and when to use toSorted().
Compare every way to loop over a JavaScript array: for...of, forEach, map, filter, reduce and for, with async patterns and common pitfalls.
Compare pop, shift, splice, filter and toSpliced for removing array elements in JavaScript, and learn why the delete operator leaves holes.
Learn push, unshift and splice for changing arrays in place, and spread, concat, toSpliced and with when you need a new array.
Array literals, Array.from, Array.of, spread and the Array constructor compared, with the sparse array pitfall and modern non-mutating methods.
Five ways to create an object in JavaScript, from the literal to classes and Object.create, with guidance on which to choose.
Objects store named key-value pairs and arrays store ordered lists. See how they differ in access, iteration, copying and type checks.
A declaration is hoisted with its body and always named, while an expression is a value created when execution reaches it. See examples and guidance.
A while loop checks first and may never run. A do-while loop runs once before checking. See examples and when to use each.
A for loop gives you break, continue and await control, while forEach is a callback method for simple side effects. See when to use each, with examples.
See how null and undefined behave differently in functions, objects, type coercion and TypeScript, and how to check for each one correctly.
== converts types before comparing, === does not. See the rules each operator follows, with examples, edge cases and practical advice.
`var` is function scoped and hoisted as undefined, while `let` is block scoped. Learn every difference with examples.
A JavaScript closure is a function that remembers the variables of the scope it was created in. See how closures enable private state, factories and callbacks.
Learn how JavaScript hoists var, let, const, classes and functions, why early access gives undefined or a ReferenceError, and how to avoid hoisting bugs.
Use const by default and let when a value changes. See how scope, hoisting and the temporal dead zone separate let and const from var.
JavaScript has eight data types: seven primitives and the object type. See each one with examples, typeof quirks and reliable type checks.