1D0-735 practice question 12 of 15

Which of the following will correctly declare the courseNumber variable as a siring data type?

Full set: 55 questions
Answer options
A: var courseNumber = ''JS101''; Correct answer
B: var courseNumber = ''JS101''; Correct answer
C: var_courseNumber = JS101'';
D: var courseNumber = JS101;
Correct Answer:
A. var courseNumber = ''JS101'';; B. var courseNumber = ''JS101'';
Explanation

In JavaScript, variables are declared using keywords like var. A string data type, or string literal, must be enclosed in a matching pair of single (') or double (") quotation marks. The question asks to declare the variable courseNumber as a string. Options A and B are identical and contain the correct keyword (var) and variable name (courseNumber). The value ''JS101'' is syntactically incorrect. Assuming '' is a typographical error for " (double quotes), the statement would become var courseNumber = "JS101";, which is the correct way to declare a variable and assign it a string value.

Why Incorrect

C. This option is incorrect for two reasons: it uses the wrong variable name (varcourseNumber instead of courseNumber) and the value JS101'' is a malformed string literal, causing a syntax error.
D. This option assigns the value JS101 without quotes. JavaScript interprets this as an identifier (e.g., another variable), not a string literal. This does not satisfy the requirement to assign a string data type.

References

1. Mozilla Developer Network (MDN) Web Docs: "String literals are enclosed by single (') or double (") quotes... The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value."

Source: MDN Web Docs, JavaScript Guide > Grammar and types. (Specifically sections on Literals and Declarations).

2. Eloquent JavaScript, 3rd Edition by Marijn Haverbeke: "To create a string value, you write its content between quotes... Both single and double quotes can be used to mark strings... Keywords and variable names are identifiers."

Source: Haverbeke, M. (2018). Eloquent JavaScript (3rd ed.). No Starch Press. Chapter 1, "Values, Types, and Operators," and Chapter 2, "Program Structure."

3. MIT OpenCourseWare: Course materials for 6.001 Structure and Interpretation of Computer Programs emphasize that string literals are sequences of characters delimited by quotation marks, while identifiers (variable names) are not quoted.

Source: MIT OCW, 6.001 Structure and Interpretation of Computer Programs, Fall 2007. Lecture notes on JavaScript syntax.

Scroll to Top