In JavaScript some types have this behavior, and variables that receive type are just references to the initial type p>
This is the case for objects, arrays, and functions. But it is not the case of numbers, strings and booleans. The problem you describe in the question does not happen with these, also called primitive types .
To solve the problem, you have different options depending on the type you are dealing with. For arrays you already have an alternative other answer , you can also use other variants that also work for objects. For complex objects you can do ideas from these answers , the simplest way (for objects or primitive-only arrays) is
var b = JSON.parse(JSON.stringify(a));
To copy functions the simplest way is to use new
, so you get a different instance, which shares prototype .