LEARNING JAVASCRIPT - Trang 120

Table 5-8. typeof return values

Expression

Return value

Notes

typeof undefined

"undefined"

typeof null

"object"

Unfortunate, but true

typeof {}

"object"

typeof true

"boolean"

typeof 1

"number"

typeof ""

"string"

typeof Symbol()

"symbol"

New in ES6

typeof function() {} "function"

Because typeof is an operator, parentheses are not required. That

is, if you have a variable x, you can use typeof x instead of
typeof(x)

. The latter is valid syntax—the parentheses just form an

unnecessary expression group.

void Operator

The

void

operator has only one job: to evaluate its operand and then return

undefined

. Sound useless? It is. It can be used to force expression evaluation where

you want a return value of

undefined

, but I have never run across such a situation in

the wild. The only reason I include it in this book is that you will occasionally see it

used as the URI for an HTML

<a>

tag, which will prevent the browser from navigat‐

ing to a new page:

<

a

href

=

"javascript:void 0"

>Do nothing.</

a

>

This is not a recommended approach, but you do see it from time to time.

Assignment Operators

The assignment operator is straightforward: it assigns a value to a variable. What’s on

the lefthand side of the equals sign (sometimes called the lvalue) must be a variable,

property, or array element. That is, it must be something that can hold a value

(assigning a value to a constant is technically part of the declaration, not an assign‐

ment operator).

96 | Chapter 5: Expressions and Operators