Javascript
JavaScript Fundamentals -- Variables and Scope

Value vs. Reference

Outline

Data is stored to variables in two manners, by value and by reference. In addition, variables are passed to functions by value or by reference.

Value:

  • The data of a variable is copied to other variables, or passed in to functions
  • Changing the data of copied variables does not change the data of the original variable
  • strings, numbers, booleans, undefined, and null are examples that use this type of assignment

Reference:

  • The variable points to a place in memory
  • Copying variables copies the location in memory to the second variable
  • Changing the data of one variable affects all variables that have the reference
  • This applies to objects and arrays

To follow along, fork this StackBlitz project.

For more information on this topic, check out this blog post.

 

I finished! On to the next chapter