Javascript Developer JSE 40 01 · Free Practice Question Easy
Question 25
Q409 - Data Types
Analyze the code snippet:
- let distance = 0;
- let userName = "John";
After declaring the distance variable,
we want to add a short comment with information
about what the variable is used for.
To do this, we modify the line with the declaration to the form:
-
A
- let distance = 0; ## the distance the user has walked
-
B
- let distance = 0; /* the distance the user has walked
-
C
- // let distance = 0; the distance the user has walked
-
D
- let distance = 0; /* the distance the user has walked */
Reveal correct answer
Correct answer: D
Explanation
Topic: multi-line comment
Try it yourself:
- let distance = 0;
- let userName = "John";
- let distance1 = 0; /* the distance the user has walked */
- // let distance2 = 0; the distance the user has walked
- // let distance3 = 0; /* the distance the user has walked
- // Uncaught SyntaxError: unterminated comment
- // let distance4 = 0; ## the distance the user has walked
- // Uncaught SyntaxError: '#' not followed by identifier
Explanation:
A single-line comment would be sufficient here
but a multi-line comment also works.
JavaScript comments can be used to explain JavaScript code,
and to make it more readable.
Multi-line comments start with /* and end with */
Any text between /* and */ will be ignored by JavaScript.
https://www.w3schools.com/js/js_comments.asp
(There are similar questions Q509 and Q609.)
Q409 (Please refer to this number, if you want to write me about this question.)
A. Using ## to add a comment is not a valid syntax in JavaScript. Comments in JavaScript should be enclosed within /* comment */ or // comment to be considered valid.
B. This choice is almost correct, but it lacks the closing */ for the comment, which would result in a syntax error. The comment itself provides the necessary information about the purpose of the variable.
C. Using // at the beginning of the line is a valid way to add a comment in JavaScript, but it should be placed after the statement, not before it. Placing the comment before the statement will result in a syntax error.
D. Adding a comment after the variable declaration using the /* comment */ syntax is the correct way to provide information about the purpose of the variable. In this case, the comment explains that the variable distance represents the distance the user has walked.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
