Syntax
arr.fill(value, start, end)
arr.copyWithin(target, start, end)Examples
fill() - Initializing an Array
A common pattern for creating an array of a fixed size with default values.
const zeros = new Array(5).fill(0);
console.log(zeros); // [0, 0, 0, 0, 0]
const partial = [1, 2, 3, 4, 5];
partial.fill(0, 1, 3); // fill with 0, starting at index 1, up to (not including) index 3
console.log(partial); // [1, 0, 0, 4, 5]copyWithin() - Copying Within the Same Array
Shifting a section of elements to overwrite another section.
const arr = [1, 2, 3, 4, 5];
arr.copyWithin(0, 3); // copy from index 3 to the end, paste starting at index 0
console.log(arr); // [4, 5, 3, 4, 5]Best practices
- Use fill() combined with new Array(n) to quickly initialize a fixed-size array with default values before populating it
- Remember fill() and copyWithin() both mutate the array in place - copy first if the original needs to stay unchanged
- copyWithin() is rarely needed in typical application code - it is more common in performance-sensitive or lower-level array manipulation
- Double-check start/end index arguments carefully, as off-by-one mistakes are easy to make with these methods
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Array Basics
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().