4Variables, Scope, and Memory
WHAT'S IN THIS CHAPTER?
- Working with primitive and reference values in variables
- Understanding execution context
- Understanding garbage collection
WROX.COM DOWNLOADS FOR THIS CHAPTER
Please note that all the code examples for this chapter are available as a part of this chapter's code download on the book's website at www.wrox.com/go/projavascript4e
on the Download Code tab.
The nature of variables in JavaScript, as defined in ECMA-262, is quite unique compared to that of other languages. Being loosely typed, a variable is literally just a name for a particular value at a particular time. Because there are no rules defining the type of data that a variable must hold, a variable's value and data type can change during the lifetime of a script. Though this is an interesting, powerful, and problematic feature, there are many more complexities related to variables.
PRIMITIVE AND REFERENCE VALUES
ECMAScript variables may contain two different types of data: primitive values and reference values. Primitive values are simple atomic pieces of data, while reference values are objects that may be made up of multiple values.
When a value is assigned to a variable, the JavaScript engine must determine if it's a primitive or a reference value. The six primitive types were discussed in the previous chapter: Undefined, Null, Boolean, Number, String, and Symbol. These variables are said to be accessed by value, because you are manipulating the actual value ...
Get Professional JavaScript for Web Developers, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.