Values

The most fundamental unit of information in a program is a value. Values are data. They’re how the program maintains state. Values come in two forms in JS: primitive and object.

Values are embedded in programs using literals:

  1. greeting("My name is Kyle.");

In this program, the value "My name is Kyle." is a primitive string literal; strings are ordered collections of characters, usually used to represent words and sentences.

I used the double-quote " character to delimit (surround, separate, define) the string value. But I could have used the single-quote ' character as well. The choice of which quote character is entirely stylistic. The important thing, for the sake of code readability and maintainability, is to pick one and to use it consistently throughout the program.

Another option to delimit a string literal is to use the back-tick ` character. However, this choice is not merely stylistic; there’s a behavioral difference as well. Consider:

  1. console.log("My name is ${ firstName }.");
  2. // My name is ${ firstName }.
  3. console.log('My name is ${ firstName }.');
  4. // My name is ${ firstName }.
  5. console.log(`My name is ${ firstName }.`);
  6. // My name is Kyle.

Assuming this program has already defined a variable firstName with the string value "Kyle", the ` -delimited string then resolves the variable expression (indicated with ${ .. }) to its current value. This is called interpolation.

The back-tick ` -delimited string can be used without including interpolated expressions, but that defeats the whole purpose of that alternate string literal syntax:

  1. console.log(
  2. `Am I confusing you by omitting interpolation?`
  3. );
  4. // Am I confusing you by omitting interpolation?

The better approach is to use " or ' (again, pick one and stick to it!) for strings unless you need interpolation; reserve ` only for strings that will include interpolated expressions.

Other than strings, JS programs often contain other primitive literal values such as booleans and numbers:

  1. while (false) {
  2. console.log(3.141592);
  3. }

while represents a loop type, a way to repeat operations while its condition is true.

In this case, the loop will never run (and nothing will be printed), because we used the false boolean value as the loop conditional. true would have resulted in a loop that keeps going forever, so be careful!

The number 3.141592 is, as you may know, an approximation of mathematical PI to the first six digits. Rather than embed such a value, however, you would typically use the predefined Math.PI value for that purpose. Another variation on numbers is the bigint (big-integer) primitive type, which is used for storing arbitrarily large numbers.

Numbers are most often used in programs for counting steps, such as loop iterations, and accessing information in numeric positions (i.e., an array index). We’ll cover arrays/objects in a little bit, but as an example, if there was an array called names, we could access the element in its second position like this:

  1. console.log(`My name is ${ names[1] }.`);
  2. // My name is Kyle.

We used 1 for the element in the second position, instead of 2, because like in most programming languages, JS array indices are 0-based (0 is the first position).

In addition to strings, numbers, and booleans, two other primitive values in JS programs are null and undefined. While there are differences between them (some historic and some contemporary), for the most part both values serve the purpose of indicating emptiness (or absence) of a value.

Many developers prefer to treat them both consistently in this fashion, which is to say that the values are assumed to be indistinguishable. If care is taken, this is often possible. However, it’s safest and best to use only undefined as the single empty value, even though null seems attractive in that it’s shorter to type!

  1. while (value != undefined) {
  2. console.log("Still got something!");
  3. }

The final primitive value to be aware of is a symbol, which is a special-purpose value that behaves as a hidden unguessable value. Symbols are almost exclusively used as special keys on objects:

  1. hitchhikersGuide[ Symbol("meaning of life") ];
  2. // 42

You won’t encounter direct usage of symbols very often in typical JS programs. They’re mostly used in low-level code such as in libraries and frameworks.

Arrays And Objects

Besides primitives, the other value type in JS is an object value.

As mentioned earlier, arrays are a special type of object that’s comprised of an ordered and numerically indexed list of data:

  1. var names = [ "Frank", "Kyle", "Peter", "Susan" ];
  2. names.length;
  3. // 4
  4. names[0];
  5. // Frank
  6. names[1];
  7. // Kyle

JS arrays can hold any value type, either primitive or object (including other arrays). As we’ll see toward the end of Chapter 3, even functions are values that can be held in arrays or objects.

NOTE:
Functions, like arrays, are a special kind (aka, sub-type) of object. We’ll cover functions in more detail in a bit.

Objects are more general: an unordered, keyed collection of any various values. In other words, you access the element by a string location name (aka “key” or “property”) rather than by its numeric position (as with arrays). For example:

  1. var me = {
  2. first: "Kyle",
  3. last: "Simpson",
  4. age: 39,
  5. specialties: [ "JS", "Table Tennis" ]
  6. };
  7. console.log(`My name is ${ me.first }.`);

Here, me represents an object, and first represents the name of a location of information in that object (value collection). Another syntax option that accesses information in an object by its property/key uses the square-brackets [ ], such as me["first"].

Value Type Determination

For distinguishing values, the typeof operator tells you its built-in type, if primitive, or "object" otherwise:

  1. typeof 42; // "number"
  2. typeof "abc"; // "string"
  3. typeof true; // "boolean"
  4. typeof undefined; // "undefined"
  5. typeof null; // "object" -- oops, bug!
  6. typeof { "a": 1 }; // "object"
  7. typeof [1,2,3]; // "object"
  8. typeof function hello(){}; // "function"
WARNING:
typeof null unfortunately returns “object” instead of the expected “null”. Also, typeof returns the specific “function” for functions, but not the expected “array” for arrays.

Converting from one value type to another, such as from string to number, is referred to in JS as “coercion.” We’ll cover this in more detail later in this chapter.

Primitive values and object values behave differently when they’re assigned or passed around. We’ll cover these details in Appendix A, “Values vs References.”