JavaScript split() vs replace(): Differences and Examples
split() turns a string into an array at each separator, while replace() returns a string with a match substituted. See replaceAll(), regex use and pitfalls.
466 posts about JavaScript: questions with answers, tutorials and articles, newest first.
split() turns a string into an array at each separator, while replace() returns a string with a match substituted. See replaceAll(), regex use and pitfalls.
slice() accepts negative indices and returns an empty string for reversed ranges, while substring() clamps negatives to 0 and swaps arguments. Examples included.
sort orders elements with an optional comparator, reverse flips the order, and both mutate the array. See the default string-sort trap and ES2023 alternatives.
slice returns a shallow copy of a range and never mutates; splice removes, replaces or inserts in place. Learn the arguments, pitfalls and toSpliced.
reduce folds an array from the first element, reduceRight from the last. Learn when direction matters, such as composition and subtraction.
filter returns every match as a new array, find returns the first match or undefined. See examples, performance notes and related methods.
map builds a new array from each element, forEach only runs side effects and returns undefined. Learn when to use each and common mistakes.
Object.assign is a built-in shallow, mutating copy, and Object.merge is not part of JavaScript. Learn what to use for shallow and deep merges.
Object.create links a new object to a prototype of your choice and runs no code. The new operator runs a constructor and links the instance to its prototype property.
Object.freeze blocks all changes to an object, while Object.seal still lets you update existing values. Compare them with examples and a deep freeze helper.
`let` allows reassignment while `const` does not, though const objects can still change. Learn when to use each.
In browsers window is the global object, in Node.js it is global, and globalThis works everywhere. See which variable declarations become global properties and why.
preventDefault cancels default browser behaviour such as form submits, while stopPropagation stops an event bubbling or capturing to other elements. Learn when to use each.
Use addEventListener for all event handling. attachEvent was an IE-only legacy API removed from modern browsers; this guide compares the two and shows migration.
Capturing goes from the root down to the target and bubbling goes back up. See the order of listeners, the capture option and when each phase is useful.
Call event.preventDefault() in a listener to cancel actions like link navigation or form submission, and understand cancelable events and passive listeners.
Use addEventListener to respond to clicks, form submits and key presses, and learn bubbling, delegation and clean listener removal.
Build an Event or CustomEvent, dispatch it on a target and listen with addEventListener. Includes bubbling, cancelling and non-DOM EventTarget examples.
A JavaScript event is a signal that something happened in the browser. See how listeners, bubbling, delegation and custom events work, with modern examples.
fetch() does not reject on 404 or 500. Learn to check response.ok, add timeouts with AbortSignal, classify errors and retry transient failures.