By Chris Minnick, Eva Holland

Forming JavaScript sentences is called writing statements. Statements are mostly made up of operands (which are like nouns) and operators (which are like verbs). An expression in JavaScript is any valid piece of code that resolves to a value.

When you say that an expression “resolves to a value,” what you mean is that when everything the computer needs to do is done, some sort of value is produced. For example,

  • The expression 1 + 1 “resolves to” 2. Another way to say the same thing is that it has the value of 2.

  • The expression x = 7 is a different kind of expression. It assigns its value (7) to the variable called x.

Expressions are made up of operands (such as the number 1 or the variable x) and operators (such as = or +). Operands can be any of the JavaScript data types, as well as objects or arrays.

Listed here are some valid JavaScript operands, and you’ll determine which type of data each operand is.

All the answers are given at first, until you get the hang of it. After that, all the answers will still be given, but they’ll be hidden a little better.

For each of the following operands, determine whether it’s a number, a string, or a Boolean:

  • 100: This is, quite clearly, a number, because it’s not surrounded by quotes and it’s made entirely of numbers.

  • Hello JavaScript World!: This is a string, because it’s surrounded by quotes.

  • false: This is a Boolean, because it’s true or false and it’s not surrounded by quotes.

  • true: This is a string, because, even though it contains the word true (which would seem to make it a Boolean), the word is surrounded by quotes (which makes it a string).

Now it’s your turn. For each of the following operands, decide whether it’s a number, a string, or a Boolean:

  • 187

  • 007

  • Number 9

  • true

  • 86

  • Its 5 oclock somewhere

How do you think you did? Here are the answers:

  • 187 is a number.

  • 007 is a string.

  • Number 9 is a string.

  • true is a Boolean.

  • 86 is a number.

  • Its 5 oclock somewhere is a string.

Operands aren’t always literal values. More often, in fact, programmers assign values to variables and use those variables as operands. Here are some statements that assign values to variables. For each of these, figure out the data type of the operand (number, string, or Boolean):

  • distance = 3000

  • distance = 800 * 4

  • doTheMath = 7 + 8 + 36 + 18 + 12

  • countrySong = mama + truck + train + rain

If you said that the first three of these are numbers and the last is a string, you got it right!

Try the following exercise to find out for yourself!

  1. Open the JavaScript console in Chrome by pressing Command+Option+J (Mac) or Ctrl+Shift+J (Windows).

    The JavaScript Console appears, as shown here.

    Opening the JavaScript Console.

    Opening the JavaScript Console.
  2. Use the typeof command to find out the data type of a number.

    For example, type this:

    typeof 8

    The JavaScript Console returns number, as shown here.

    Finding out the type of an operand.

    Finding out the type of an operand.
  3. Type the following to create a variable and use an expression to set its value:

    var doTheMath = 7 + 8 + 36 + 18 + 12

    The console returns undefined, letting you know that it has done its job.

  4. Find out the type of your new variable using the typeof command.

    typeof doTheMath

    The result, once again, is number.

  5. Find out the type of a string with the following expression:

    typeof "the cat's favorite toy"

    The result, of course, is string.

  6. Try finding the type of a word that’s not in quotes, like this:

    typeof automobile

    The result of this expression will be undefined. What’s happening here is that JavaScript treats a word that’s not in quotes as if it’s a variable. Because you haven’t defined a variable named automobile, the result of asking for the type of the automobile variable is undefined.