0

I need one clarification java script. I used two variables in my program. I stored particular array in first variable and again create one variable. I have stored second variable value in first variable.Then I pushed one vale in second variable... If i print first variable means second variable vale displayed.. My expectation is first variable value don't want to change..

 // first variable
 var test = ["a","b","c"];  

// second variable
var arr = test;
arr.unshift("d");

// second variable print
console.log(arr);  // ["a","b","c","d"]

// First variable print
    console.log(test); // ["a","b","c","d"]

// I want to maintain first original value
// Expectation result  
 console.log(test); // ["a","b","c"]

I need maintain original value in first variable..What I do?

0

1 Answer 1

0

You can use the spread operator (check browser capability)

var arr = [...test];

 // first variable
 var test = ["a","b","c"];  

// second variable
var arr = [...test];
arr.push("d");

// second variable print
console.log(arr);  // ["a","b","c","d"]

// First variable print
    console.log(test); // ["a","b","c","d"]

// I want to maintain first original value
// Expectation result  
 console.log(test); // ["a","b","c"]

Sign up to request clarification or add additional context in comments.

1 Comment

Dynamically we want work it..Just give the example for that code sir..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.