Chapter 1
Chapter 1
Chapter 1
Values, Types and Operators
Values 2
It isn’t mathematically sound, and it will quickly lead to the next special
number: NaN.
NaN stands for “not a number”, even though it is a value of the number
type.
You’ll get this result when you, for example, try to calculate 0 / 0 (zero
divided by zero), Infinity - Infinity, or any number of other numeric
operations that don’t yield a meaningful result.
9
Strings
The next basic data type is the string. Strings are used to represent text. They are
written by enclosing their content in quotes.
`Down on the sea`
"Lie on the ocean“
’ Float on the ocean'
You can use single quotes, double quotes, or backticks to mark strings, as long as
the quotes at the start and the end of the string match.
10
Cont.…
Almost anything can be put between quotes, and JavaScript will make a string value
out of it. But a few characters are more difficult.
You can imagine how putting quotes between quotes might be hard. Newlines (the
characters you get when you press enter) can be included without escaping only when
the string is quoted with backticks (\‘).
To make it possible to include such characters in a string, the following
notation is used: whenever a backslash (\) is found inside quoted text, it
indicates that the character after it has a special meaning. This is called
escaping the character. A quote that is preceded by a backslash will not end the string
but be part of it.
Cont.… 11
Not all operators are symbols. Some are written as words. One
example is the typeof operator, which produces a string value
naming the type of the value you give it.
console.log(typeof 4.5)
// → number
console.log(typeof "x")
// → string
Cont.… 15
The other operators shown all operated on two values, but typeof
takes only one.
Operators that use two values are called binary operators, while
those that take one are called unary operators. The minus operator
can be used both as a binary operator and as a unary operator.
console.log(- (10 - 2))
// → -8
Boolean values 16
When comparing strings, JavaScript goes over the characters from left to right, comparing
the Unicode codes one by one.
Other similar operators are >= (greater than or equal to), <= (less than or equal to), ==
(equal to), and != (not equal to).
console.log("Itchy" != "Scratchy")
// → true
console.log("Apple" == "Orange")
// → false.
There is only one value in JavaScript that is not equal to itself, and that is
NaN (“not a number”).
console.log(NaN == NaN)
// → false
Logical operators 19
There are also some operations that can be applied to Boolean values
themselves.
JavaScript supports three logical operators: and, or, and not.
These can be used to “reason” about Booleans.
The && operator represents logical and. It is a binary operator, and its
result is true only if both the values given to it are true.
console.log(true && false)
// → false
console.log(true && true)
// → true
20
console.log(true ? 1 : 2);
// → 1
console.log(false ? 1 : 2);
// → 2
Empty values 22
There are two special values, written null and undefined, that are
used to denote the absence of a meaningful value.
They are themselves values, but they carry no information.
Automatic type conversion 23
In the Introduction, I mentioned that JavaScript goes out of its way to accept
almost any program you give it, even programs that do odd things.
This is nicely demonstrated by the following expressions:
console.log(8 * null)
// → 0
console.log("5" - 1)
// → 4
console.log("5" + 1)
// → 51
console.log("five" * 2)
// → NaN
console.log(false == 0)
// → true
Cont.… 24
The logical operators && and || handle values of different types in a peculiar way.
They will convert the value on their left side to Boolean type in order to decide
what to do, but depending on the operator and the result of that conversion, they
will return either the original left-hand value or the righthand value.
This has the expected effect when the values are Boolean and does something
analogous for values of other types.
console.log(null || "user")
// → user
console.log("Agnes" || "user")
// → Agnes
Summary 26
We looked at four types of JavaScript values in this chapter: numbers, strings, Booleans, and undefined
values.
Such values are created by typing in their name (true, null) or value (13
, "abc"). You can combine and transform values with operators.
We saw binary operators for arithmetic (+, -, *, /, and %), string concatenation (+),
comparison (==, !=, ===, !==, <, >, <=, >=), and logic (&&, ||), as well as several
unary operators (- to negate a number, ! to negate logically, and typeof to
find a value’s type) and a ternary operator (?:) to pick one of two values based
on a third value.
This gives you enough information to use JavaScript as a pocket calculator
but not much more.
The next chapter will start tying these expressions together
into basic programs.
27
END