0

I'm having a little problem, I have a variable called 'widgets' and I want to store it's original value in 'storeWigets' and even if I update the value of 'widgets' my variable 'storeWidgets' stays with the original value of 'widgets'.

var widgets=["1","2","3","4"];
var storeWidgets;
storeWidgets = widgets;
widgets[0]=0;
alert(storeWidgets);

I have an example on jsfiddle, I need that in the end my 'storeWidgets' keeps the value ["1","2","3","4"] but it keeps returning me ["0","2","3","4"]

andradegbz
  • 27
  • 7

1 Answers1

-1

Try this

var widgets=["1","2","3","4"];
var storeWidgets;
storeWidgets = widgets.slice(0);;
widgets[0]=0;
alert(widgets);
alert(storeWidgets);
Sourabh Somani
  • 2,138
  • 1
  • 13
  • 27