Types and Scope - SelfishHellfish/JS_Ref GitHub Wiki

Primitive vs. Reference Types

When assigned to variables, primitive types like numbers, strings, and booleans are directly associated with values in memory, however, reference types, such as arrays, are associated with pointers that point to their respective values, to avoid the copying of complex and memory-cluttering objects. Redefining a primitive type variable will create a new pointer to a new value, and any prior variable copies will retain their original pointers to the original value. E.g, modifying an array will affect the value of any copies it may have, since with reference types, the pointers of all variables will be pointing to the same object. Essentially, mutable objects will always have the potential for generating bugs if multiple variables point to them.
var a = [1,2,3]; var b = a; console.log(b); a[0] = 5; console.log(b); > [1, 2, 3] [5, 2, 3]

Scope

Variables in functions are in a local scope and cannot be accessed outside of them. Global variables can be accessed anywhere. Types and scope cheat sheet