Open In App

How to Copy Array by Value in JavaScript ?

Last Updated : 11 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

There are various methods to copy array by value in JavaScript.

1. Using Spread Operator

The JavaScript spread operator is a concise and easy metho to copy an array by value. The spread operator allows you to expand an array into individual elements, which can then be used to create a new array.

Syntax

const newArr = [ ...arr];
JavaScript
// Given array
const a = [ 10, 20, 30, 40, 50 ];

// Copy the array using spread operator
const a1 = [...a];

// Display new array
console.log("New Array: ", a1);

// Check if the arrays share same reference 
console.log("Is Both Arrays has Same Refere