Sams Teach Yourself JavaScript in 24 Hours

Sams Teach Yourself JavaScript in 24 Hours

By Michael Moncur

Quiz

Test your knowledge of JavaScript variables and expressions by answering the following questions.

Questions

1:

Which of the following is not a valid JavaScript variable name?

  1. 2names
  2. _first_and_last_names
  3. FirstAndLast
A1:

a. 2names is an invalid JavaScript variable name since it begins with a number. The others are valid, although they're probably not ideal choices for names.

2:

If the statement var fig=2 appears in a function, which type of variable does it declare?

  1. A global variable
  2. A local variable
  3. A constant variable
A2:

b. Since the variable is declared in a function, it is a local variable. The var keyword ensures that a local variable is created.

3:

What will be the result of the JavaScript expression 31 + " angry polar bears"?

  1. An error message
  2. 32
  3. "31 angry polar bears"
A3:

c. JavaScript converts the whole expression to the string "31 angry polar bears". (No offense to polar bears, who are seldom angry and rarely seen in groups this large.)

Share ThisShare This

Informit Network