primitive values (
null, booleans, numbers, and
strings) and objects (including arrays and functions). Primitives are
immutable: there is no way to change (or “mutate”) a primitive value.
This is obvious for numbers and booleans—it doesn’t even make sense to
change the value of a number. It is not so obvious for strings,
however. Since strings are like arrays of characters, you might expect
to be able to alter the character at any specified index. In fact,
return a modified string are, in fact, returning a new string value.
// Start with some lowercase text
// Returns "HELLO", but doesn't alter s
// => "hello": the original string has not changed
Primitives are also compared by value: two
values are the same only if they have the same value. This sounds
circular for numbers, booleans,
undefined: there is no other way that they
could be compared. Again, however, it is not so obvious for strings.
equal if, and only if, they have the same length and if the character
at each index is the same.
Objects are different than primitives. First, they are mutable—their values can change:
// Start with an object
// Mutate it by changing the value of a property ...