Which example is a valid variable name in JavaScript?

Advance your skills with the uCertify CIW – Advanced HTML5 and CSS3 Specialist Test. Study with flashcards and multiple-choice questions, complete with hints and explanations to enhance your learning. Prepare effectively and ace your exam!

Multiple Choice

Which example is a valid variable name in JavaScript?

Explanation:
In JavaScript, an identifier (a variable name) must start with a letter, underscore, or dollar sign, and can contain letters, digits, underscores, or dollar signs after the first character. The example that begins with an underscore fits these rules: it starts with an allowed character and contains only letters and underscores, so it’s a valid variable name. The other examples fail for these reasons: starting with a digit isn’t allowed, so the one that begins with a number can’t be a variable name; using a hyphen is invalid because the hyphen is treated as subtraction, not part of an identifier.

In JavaScript, an identifier (a variable name) must start with a letter, underscore, or dollar sign, and can contain letters, digits, underscores, or dollar signs after the first character. The example that begins with an underscore fits these rules: it starts with an allowed character and contains only letters and underscores, so it’s a valid variable name.

The other examples fail for these reasons: starting with a digit isn’t allowed, so the one that begins with a number can’t be a variable name; using a hyphen is invalid because the hyphen is treated as subtraction, not part of an identifier.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy