Javascript Developer JSE 40 01 · Free Practice Question Easy
Question 11
Q414 - Data Types
Using the string interpolation technique, we can create the string
"I do not like travelling by plane"
and store it in the msg variable using the command:
-
A
- let means = "plane";
- let msg =
I do not like travelling by ${means};
-
B
- let means = "plane";
- let msg = "I do not like travelling by ${ means }";
-
C
- let means = "plane";
- let msg = 'I do not like travelling by {means}';
-
D
- let means = "plane";
- let msg = "I do not like travelling by \{ means }\";
Reveal correct answer
Correct answer: A
Explanation
Topic: template literals
Try it yourself:
- let msg1 =
I do not like travelling by ${means};- console.log(msg1); // I do not like travelling by plane
- let msg2 = 'I do not like travelling by {means}';
- console.log(msg2); // I do not like travelling by {means}
- let msg3 = "I do not like travelling by ${ means }";
- console.log(msg3); // I do not like travelling by ${ means }
- // let msg4 = "I do not like travelling by \{ means }\";
- // Uncaught SyntaxError: "" string literal contains an unescaped line break
Explanation:
Template literals use back-ticks (``) to define a string.
Template literals provide an easy way
to interpolate variables and expressions into strings.
https://www.w3schools.com/js/js_string_templates.asp
(There is a similar question Q314.)
Q414 (Please refer to this number, if you want to write me about this question.)
A. This choice correctly uses the string interpolation syntax ${} to insert the value of the variable means into the string. This results in the desired string "I do not like travelling by plane" being stored in the msg variable.
B. This choice uses double quotes for the string but does not use backticks for string interpolation. The variable means is not correctly inserted into the string, resulting in the literal string "I do not like travelling by ${ means }" being stored in the msg variable.
C. This choice uses single quotes instead of backticks for the string, which does not allow for string interpolation. The variable means is not correctly inserted into the string, resulting in the literal string "I do not like travelling by {means}" being stored in the msg variable.
D. This choice uses double quotes for the string but attempts to escape the curly braces around the variable means, which is unnecessary and incorrect. The backslash before the curly braces and the extra double quote at the end make the syntax invalid, resulting in an incorrect string being stored in the msg variable.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
