• Skip to main content
  • Skip to search
  • Skip to select language
HTML

Structure of content on the web

  • Web APIs

    Interfaces for building web applications

  • Learn
    • CSS

      Learn to style content using CSS

    • Overview

      A customized MDN experience

    • FAQ

      Frequently asked questions about MDN Plus

  • HTTP Observatory

    Scan a website for free

  • JavaScript
  • Array.prototype.copyWithin()
      • Deutsch
      • Español
      • Français
      • 日本語
      • 한국어
      • Português (do Brasil)
      • Русский
      • 中文 (简体)
      • 正體中文 (繁體)

    In this article

    • Try it
    • Syntax
    • Description
    • Examples
    • Specifications
    • Browser compatibility
    • See also
    1. Array.from()
    2. Array[Symbol.species]
  • Instance methods
    1. Array.prototype.entries()
    2. Array.prototype.find()
    3. Array.prototype.flat()
    4. Array.prototype.indexOf()
    5. Array.prototype.map()
    6. Array.prototype.reduceRight()
    7. Array.prototype.some()
    8. Array.prototype.toReversed()
    9. Array.prototype.unshift()
    10. Array: length
    11. Function.prototype.bind()
    12. Function: displayName Non-standard
    13. Function.prototype.arguments Non-standard Deprecated
    14. Function.prototype.caller Non-standard Deprecated
  • Instance methods
    1. Object.prototype.__defineGetter__() Deprecated
    2. Object.prototype.__defineSetter__() Deprecated
    3. Object.prototype.__lookupGetter__() Deprecated
    4. Object.prototype.__lookupSetter__() Deprecated
    5. Object.prototype.toLocaleString()
    6. Object.prototype.__proto__ Deprecated
    7. Array instances shallow copies part of this array to another location in the same array and returns this array without modifying its length.

  • Try it

    const array1 = ["a", "b", "c", "d", "e"];
    
    / Copy to index 0 the element at index 3
    console.log(array1.copyWithin(0, 3, 4));
    / Expected output: Array ["d", "b", "c", "d", "e"]
    
    / Copy to index 1 all elements from index 3 to the end
    console.log(array1.copyWithin(1, 3));
    / Expected output: Array ["d", "d", "e", "d", "e"]
    

    Syntax

    js
    copyWithin(target, start)
    copyWithin(target, start, end)
    

    Parameters

    target

    Zero-based index at which to copy the sequence to, converted to an integer. This corresponds to where the element at start will be copied to, and all elements between start and end are copied to succeeding indices.

    • Negative index counts back from the end of the array — if -array.length <= target < 0, target + array.length is used.
    • If target < -array.length, 0 is used.
    • If target >= array.length, nothing is copied.
    • If target is positioned after start after normalization, copying only happens until the end of array.length (in other words, copyWithin() never extends the array).
    start

    Zero-based index at which to start copying elements from, converted to an integer.

    • Negative index counts back from the end of the array — if -array.length <= start < 0, start + array.length is used.
    • If start < -array.length, 0 is used.
    • If start >= array.length, nothing is copied.
    end Optional

    Zero-based index at which to end copying elements from, converted to an integer. copyWithin() copies up to but not including end.

    • Negative index counts back from the end of the array — if -array.length <= end < 0, end + array.length is used.
    • If end < -array.length, 0 is used.
    • If end >= array.length or end is omitted or undefined, array.length is used, causing all elements until the end to be copied.
    • If end implies a position before or at the position that start implies, nothing is copied.

    Return value

    The modified array.

    Description

    The copyWithin() method works like C and C++'s memmove, and is a high-performance method to shift the data of an TypedArray method of the same name. The sequence is copied and pasted as one operation; the pasted sequence will have the copied values even when the copy and paste region overlap.

    Because undefined becomes 0 when converted to an integer, omitting the start parameter has the same effect as passing 0, which copies the entire array to the target position, equivalent to a right shift where the right boundary is clipped off and the left boundary is duplicated. This behavior may confuse readers of your code, so you should explicitly pass 0 as start instead.

    js
    console.log([1, 2, 3, 4, 5].copyWithin(2));
    / [1, 2, 1, 2, 3]; move all elements to the right by 2 positions
    

    The copyWithin() method is a mutating method. It does not alter the length of this, but it will change the content of this and create new properties or delete existing properties, if necessary.

    The copyWithin() method preserves empty slots. If the region to be copied from is deleted and also become empty slots.

    The copyWithin() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.

    Examples

    Using copyWithin()

    js
    console.log([1, 2, 3, 4, 5].copyWithin(0, 3));
    / [4, 5, 3, 4, 5]
    
    console.log([1, 2, 3, 4, 5].copyWithin(0, 3, 4));
    / [4, 2, 3, 4, 5]
    
    console.log([1, 2, 3, 4, 5].copyWithin(-2, -3, -1));
    / [1, 2, 3, 3, 4]
    

    Using copyWithin() on sparse arrays

    copyWithin() will propagate empty slots.

    js
    console.log([1, , 3].copyWithin(2, 1, 2)); / [1, empty, empty]
    

    Calling copyWithin() on non-array objects

    The copyWithin() method reads the length property of this and then manipulates the integer indices involved.

    js
    const arrayLike = {
      length: 5,
      3: 1,
    };
    console.log(Array.prototype.copyWithin.call(arrayLike, 0, 3));
    / { '0': 1, '3': 1, length: 5 }
    console.log(Array.prototype.copyWithin.call(arrayLike, 3, 1));
    / { '0': 1, length: 5 }
    / The '3' property is deleted because the copied source is an empty slot
    

    Specifications

    Specification
    ECMAScript® 2026 Language Specification
    # sec-array.prototype.copywithin

    Browser compatibility

    See also

    • Polyfill of Array.prototype.copyWithin in core-js
    • es-shims polyfill of Array.prototype.copyWithin
    • Indexed collections guide
    • Array
    • TypedArray.prototype.copyWithin()

    Help improve MDN

    copyWithin() method of arrays and typed arrays shifts or copies items of an array to another index of the array without changing its length.","name":"Array copyWithin()"}},"browserCompat":["javascript.builtins.Array.copyWithin"],"pageType":"javascript-instance-method"}}

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant