When you speak or write in a human language, you
typically string together nouns and verbs to create action. You
might then spice things up with adjectives, adverbs, pronouns,
conjunctions, prepositions, and interjections. However, the real
work of a sentence is done by the nouns and verbs.
Forming JavaScript sentences is called writing statements.
Statements are mostly made up of operands (which are like
nouns) and operators (which are like verbs).
In this chapter, you learn about the different types of operands
and you see examples of how to work with operands in
JavaScript programs.
Knowing Your Operands
An expression in JavaScript is any valid piece of code that
resolves to a value.
When we say that an expression “resolves to a value,” what we
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 that we talk about in Chapter 3,
as well as objects or arrays.
In this chapter, we show you how to create and use your own
objects. We show you how to create and use arrays in Chapter 11.
Instead of simply explaining the different types of possible data
types that operands can use, let’s play a game. We’re going to list
some valid JavaScript operands, and you’ll determine which type
of data each operand is.
We’ll give you all the answers at first, until you get the hang of it.
After that, we’ll still give you all the answers, but we’ll hide them a
little better. Here we go!
For each of the following operands, tell us 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.
Chapter 8: Building Your Dream Car with Operands 127
✓ 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
✓ "It's 5 o'clock 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.
✓ "It's 5 o'clock 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!
If you don’t believe us, try the following exercise to find out for
yourself!
Информация по комментариям в разработке