Home > Article > Web Front-end > Discussion on whether values in JavaScript are passed by value or by reference_javascript skills
I encountered an interesting question recently: "Are values in JS passed by value or by reference?"
Before analyzing this problem, we need to understand what is call by value and what is call by reference. In computer science, this part is called Evaluation Strategy. It determines how values are passed between variables and between actual parameters and formal parameters when calling functions.
Pass by value VS. Pass by reference
Call by value is the most commonly used evaluation strategy: the formal parameters of a function are copies of the actual parameters passed when called. Changing the value of the formal parameter does not affect the actual parameter.
When passing by reference (call by reference), the formal parameters of the function receive implicit references to the actual parameters, rather than copies. This means that if the values of function parameters are modified, the actual parameters will also be modified. At the same time both point to the same value.
Passing by reference makes it more difficult to trace function calls and sometimes causes subtle bugs.
Passing by value requires a clone every time, so the performance is lower for some complex types. Both methods of passing values have their own problems.
Let’s first look at a C example to understand the difference between passing by value and reference:
Here we can see:
a => When p is passed by value, modifying the value of formal parameter p does not affect the actual parameter a, which is just a copy of a.
b => q is passed by reference. Modifying the value of the formal parameter q also affects the value of the actual parameter b.
Explore how JS values are passed
The basic types of JS are passed by value.
To be precise, basic types in JS are passed by value, and object types are passed by sharing (call by sharing, also called passing by object and passing by object sharing). It was first proposed by Barbara Liskov in the GLU language in 1974. This evaluation strategy is used in Python, Java, Ruby, JS and other languages.
The key point of this strategy is: when calling a function to pass parameters, the function accepts a copy of the object argument reference (neither a copy of the object passed by value, nor an implicit reference passed by reference). The difference between it and pass by reference is that the assignment of function parameters in shared transfer will not affect the value of the actual parameter. As in the following example, the value of obj cannot be modified by modifying the value of the formal parameter o.
Basic types are immutable (immutable), only objects are mutable (mutable). For example, the numerical value 100, Boolean values true, false, modify these values (for example, change 1 to 3, change true to 100 ) has no meaning. What is easier to misunderstand is string in JS. Sometimes we try to "change" the contents of a string, but in JS, any operation that appears to "modify" a string value actually creates a new string value.