Which example is a valid variable name in JavaScript?

Prepare for the uCertify CIW Advanced HTML5 and CSS3 Specialist Exam. Dive into essential topics with flashcards and multiple-choice questions. Enhance your understanding with hints and explanations for each question. Pass your exam confidently!

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