Values are embedded in programs using literals:

    In this program, the value 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. );
    3. // 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.

    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. }

    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.

    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. // Frank
    5. names[1];
    6. // 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.

    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:

    WARNING:
    typeof null unfortunately returns “object” instead of the expected “null”. Also, typeof returns the specific “function” for functions, but not the expected for arrays.

    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.”