Template literals are a feature in JavaScript, introduced with ES6, that allow you to create strings that are more flexible and powerful than traditional string concatenation. They are enclosed by backticks (`) instead of single (') or double (") quotes.
Key Features of Template Literals:
1. String Interpolation:
○ Allows embedding expressions directly within the string using the ${expression} syntax.
○ Example:
const name = 'John';
const age = 30;
const greeting =
console.log(greeting);
// Output: Hello, my name is John and I am 30 years old.
1. Multiline Strings:
○ Template literals can span multiple lines without needing escape characters (\n).
○ Example:
const multiline = `This is a string
that spans across
multiple lines.`;
console.log(multiline);
Output--
1. Expression Evaluation:
○ Any valid JavaScript expression can be included inside the ${} placeholder.
○ Example:
const a = 5;
const b = 10;
console.log(`The sum of ${a} and ${b} is ${a + b}.`);
// Output: The sum of 5 and 10 is 15.
2. Tagged Templates:
○ A way to process template literals with a function.
○ Example:
function tag(strings, ...values) {
return strings[0] + values.map((val) => val.toUpperCase()).join("");
}
const result = tag`Hello ${"world"}!`;
console.log(result);
// Output: Hello WORLD!
3. Dynamic HTML or Other Markup:
○ Useful for dynamically constructing HTML or other content.
○ Example:
const title = 'Welcome';
const content =
console.log(content);
Syntax:
Template literals are especially useful for creating complex strings and enhancing code readability.
Key Features of Template Literals:
1. String Interpolation:
○ Allows embedding expressions directly within the string using the ${expression} syntax.
○ Example:
const name = 'John';
const age = 30;
const greeting =
Hello, my name is ${name} and I am ${age} years old.; console.log(greeting);
// Output: Hello, my name is John and I am 30 years old.
1. Multiline Strings:
○ Template literals can span multiple lines without needing escape characters (\n).
○ Example:
const multiline = `This is a string
that spans across
multiple lines.`;
console.log(multiline);
Output--
1. Expression Evaluation:
○ Any valid JavaScript expression can be included inside the ${} placeholder.
○ Example:
const a = 5;
const b = 10;
console.log(`The sum of ${a} and ${b} is ${a + b}.`);
// Output: The sum of 5 and 10 is 15.
2. Tagged Templates:
○ A way to process template literals with a function.
○ Example:
function tag(strings, ...values) {
return strings[0] + values.map((val) => val.toUpperCase()).join("");
}
const result = tag`Hello ${"world"}!`;
console.log(result);
// Output: Hello WORLD!
3. Dynamic HTML or Other Markup:
○ Useful for dynamically constructing HTML or other content.
○ Example:
const title = 'Welcome';
const content =
<h1>${title}</h1>; console.log(content);
Syntax:
string text string text ${expression} string text Template literals are especially useful for creating complex strings and enhancing code readability.
👍2❤1
Here are 20 JavaScript multiple-choice interview questions along with answers:
---
### 1. What is the output of the following code?
- A.
- B.
- C.
- D.
Answer: B.
---
### 2. Which of the following is not a primitive data type in JavaScript?
- A. Number
- B. Object
- C. String
- D. Boolean
Answer: B. Object
---
### 3. What is the correct syntax to create an array in JavaScript?
- A.
- B.
- C.
- D. Both B and C
Answer: D. Both B and C
---
### 4. Which of the following methods is used to add an element to the end of an array?
- A.
- B.
- C.
- D.
Answer: A.
---
### 5. What is the output of the following code?
- A.
- B.
- C.
- D.
Answer: A.
---
### 6. How do you write an arrow function in JavaScript?
- A.
- B.
- C.
- D.
Answer: B.
---
### 7. What will `console.log([] == false)` output?
- A.
- B.
- C.
- D.
Answer: A.
---
### 8. Which keyword is used to declare a constant variable in JavaScript?
- A.
- B.
- C.
- D.
Answer: D.
---
### 9. What is the purpose of `isNaN()` in JavaScript?
- A. Check if a value is a number
- B. Check if a value is not a number
- C. Check if a value is undefined
- D. Check if a value is null
Answer: B. Check if a value is not a number
---
### 10. What does `==` compare in JavaScript?
- A. Value only
- B. Type only
- C. Value and type
- D. None of the above
Answer: A. Value only
---
### 11. Which of the following is used to stop event propagation?
- A.
- B.
- C.
- D.
Answer: A.
---
### 12. What is the purpose of `JSON.stringify()`?
- A. Convert a JSON string to an object
- B. Convert an object to a JSON string
- C. Parse a URL string
- D. None of the above
Answer: B. Convert an object to a JSON string
---
### 13. Which method is used to combine two or more arrays in JavaScript?
- A.
- B.
- C.
- D.
Answer: A.
---
### 14. What is the output of the following code?
- A.
- B.
- C.
- D.
Answer: A.
---
### 15. Which of the following is a falsy value in JavaScript?
- A.
- B.
- C.
- D. All of the above
Answer: D. All of the above
---
### 16. What is a closure in JavaScript?
- A. A function inside an object
- B. A function bundled with its lexical environment
- C. A block of code inside curly braces
- D. None of the above
Answer: B. A function bundled with its lexical environment
---
### 17. How can you check if a variable is an array?
- A.
- B.
- C.
- D. Both B and C
Answer: D. Both B and C
---
### 18. What is the purpose of `setTimeout()`?
- A. Repeats execution of code at specified intervals
- B. Executes a function after a delay
- C. Stops the execution of code
- D. None of the above
Answer: B. Executes a function after a delay
---
### 19. What will `console.log(!!"")` output?
- A.
- B.
- C.
- D.
Answer: B.
---
### **20. Which JavaScript feature
---
### 1. What is the output of the following code?
javascript
console.log(typeof null);
- A.
"null" - B.
"object" - C.
"undefined" - D.
"number" Answer: B.
"object" ---
### 2. Which of the following is not a primitive data type in JavaScript?
- A. Number
- B. Object
- C. String
- D. Boolean
Answer: B. Object
---
### 3. What is the correct syntax to create an array in JavaScript?
- A.
var arr = {}; - B.
var arr = []; - C.
var arr = new Array(); - D. Both B and C
Answer: D. Both B and C
---
### 4. Which of the following methods is used to add an element to the end of an array?
- A.
push() - B.
pop() - C.
shift() - D.
unshift() Answer: A.
push() ---
### 5. What is the output of the following code?
javascript
console.log(2 + "2");
- A.
22 - B.
4 - C.
NaN - D.
Error Answer: A.
22 ---
### 6. How do you write an arrow function in JavaScript?
- A.
function => () {} - B.
() => {} - C.
=> function {} - D.
() -> {} Answer: B.
() => {} ---
### 7. What will `console.log([] == false)` output?
- A.
true - B.
false - C.
undefined - D.
Error Answer: A.
true ---
### 8. Which keyword is used to declare a constant variable in JavaScript?
- A.
let - B.
var - C.
constant - D.
const Answer: D.
const ---
### 9. What is the purpose of `isNaN()` in JavaScript?
- A. Check if a value is a number
- B. Check if a value is not a number
- C. Check if a value is undefined
- D. Check if a value is null
Answer: B. Check if a value is not a number
---
### 10. What does `==` compare in JavaScript?
- A. Value only
- B. Type only
- C. Value and type
- D. None of the above
Answer: A. Value only
---
### 11. Which of the following is used to stop event propagation?
- A.
stopPropagation() - B.
preventDefault() - C.
return false; - D.
clearPropagation() Answer: A.
stopPropagation() ---
### 12. What is the purpose of `JSON.stringify()`?
- A. Convert a JSON string to an object
- B. Convert an object to a JSON string
- C. Parse a URL string
- D. None of the above
Answer: B. Convert an object to a JSON string
---
### 13. Which method is used to combine two or more arrays in JavaScript?
- A.
concat() - B.
merge() - C.
join() - D.
add() Answer: A.
concat() ---
### 14. What is the output of the following code?
javascript
console.log("5" - 2);
- A.
3 - B.
52 - C.
"5-2" - D.
Error Answer: A.
3 ---
### 15. Which of the following is a falsy value in JavaScript?
- A.
0 - B.
"" - C.
null - D. All of the above
Answer: D. All of the above
---
### 16. What is a closure in JavaScript?
- A. A function inside an object
- B. A function bundled with its lexical environment
- C. A block of code inside curly braces
- D. None of the above
Answer: B. A function bundled with its lexical environment
---
### 17. How can you check if a variable is an array?
- A.
typeof variable === "array" - B.
Array.isArray(variable) - C.
variable instanceof Array - D. Both B and C
Answer: D. Both B and C
---
### 18. What is the purpose of `setTimeout()`?
- A. Repeats execution of code at specified intervals
- B. Executes a function after a delay
- C. Stops the execution of code
- D. None of the above
Answer: B. Executes a function after a delay
---
### 19. What will `console.log(!!"")` output?
- A.
true - B.
false - C.
undefined - D.
Error Answer: B.
false ---
### **20. Which JavaScript feature
❤1👍1
allows functions to operate on objects irrespective of their type?**
- A. Closures
- B. Prototypes
- C. Polymorphism
- D. Hoisting
Answer: C. Polymorphism
---
- A. Closures
- B. Prototypes
- C. Polymorphism
- D. Hoisting
Answer: C. Polymorphism
---
👍5
1. What is the output of the following code?
- A) "object"
- B) "null"
- C) "undefined"
- D) "boolean"
Answer: A) "object"
2. Which of the following is not a JavaScript data type?
- A) String
- B) Number
- C) Boolean
- D) Float
Answer: D) Float
3. What will the following code output?
- A) true, true
- B) true, false
- C) false, true
- D) false, false
Answer: B) true, false
4. **What is the purpose of the
- A) To declare a block-scoped variable
- B) To declare a global variable
- C) To declare a constant
- D) To declare a function
Answer: A) To declare a block-scoped variablele**
What will the following code output?t?**
- A) "122"
- B) "32"
- C) "14"
- D) "5"
Answer: A) "122"2"**
Which method is used to add an element to the end of an array?y?**
- A)
- B)
- C)
- D)
**Answer: A)
7. **What is the output of the following code?**
- A) "number"
- B) "NaN"
- C) "undefined"
- D) "Answer: A) "number") "number"**
8. **What does the
- A) The function itself
- B) The global object
- C) The object that the method belongs to
- D) The paAnswer: C) The object that the method belongs tohod bWhat will the following code output?g code output?**
- A) "undefined"
- B) "null"
- C) ""
Answer: C) ""*AnsweWhich of the following is not a valid way to create a JavaScript object?Script object?**
- A)
- B)
- C)
- D)
**Answer: D)
11. **What is the output of the following code?**
- A) true, true
- B) true, false
- C) false, true
Answer: A) true, true*Answer: A) true, true**
12. **What is the purpose of the
- A) To bind a function to an object
- B) To bind an event to a function
- C) To bind a variable to a function
- D) To bind a Answer: A) To bind a function to an objectnd a fWhat will the following code output?the following code output?**
- A) "undefined"
- B) "null"
- C) "objeAnswer: A) "undefined" **AnWhich of the following is not a JavaScript framework or library?ript framework or library?**
- A) React
- B) Angular
- Answer: D) Djangoo
What is the output of the following code?put of the following code?**
- A) "21"
- B) "22"
Answer: C) 21 D) 3
**Answer: C) 21**
16. **What is the purpose of the
- A) To create a new array by applying a function to each element of an existing array
- B) To filter elements of an array based on a condition
- C) To reduce an array to a single value
- D) To sorAnswer: A) To create a new array by applying a function to each element of an existing arrayh elemWhat will the following code output?ill the following code output?**
- A) "array"
- B) "object"
- C) "Answer: B) "object"ll"
Which of the following is not a valid JavaScript loop?s not a valid JavaScript loop?**
- A)
- B)
- C)
- D)
**Answer: D)
console.log(typeof null);
- A) "object"
- B) "null"
- C) "undefined"
- D) "boolean"
Answer: A) "object"
2. Which of the following is not a JavaScript data type?
- A) String
- B) Number
- C) Boolean
- D) Float
Answer: D) Float
3. What will the following code output?
console.log(0 == '0');
console.log(0 === '0');
- A) true, true
- B) true, false
- C) false, true
- D) false, false
Answer: B) true, false
4. **What is the purpose of the
let keyword in JavaScript?**- A) To declare a block-scoped variable
- B) To declare a global variable
- C) To declare a constant
- D) To declare a function
Answer: A) To declare a block-scoped variablele**
What will the following code output?t?**
console.log(1 + "2" + "2");
- A) "122"
- B) "32"
- C) "14"
- D) "5"
Answer: A) "122"2"**
Which method is used to add an element to the end of an array?y?**
- A)
push()- B)
pop()- C)
shift()- D)
unshift()**Answer: A)
push()**7. **What is the output of the following code?**
console.log(typeof NaN);
- A) "number"
- B) "NaN"
- C) "undefined"
- D) "Answer: A) "number") "number"**
8. **What does the
this keyword refer to in a JavaScript method?**- A) The function itself
- B) The global object
- C) The object that the method belongs to
- D) The paAnswer: C) The object that the method belongs tohod bWhat will the following code output?g code output?**
console.log([] + []);
- A) "undefined"
- B) "null"
- C) ""
Answer: C) ""*AnsweWhich of the following is not a valid way to create a JavaScript object?Script object?**
- A)
let obj = {};- B)
let obj = new Object();- C)
let obj = Object.create(null);- D)
let obj = new Object(null);**Answer: D)
let obj = new Object(null);**11. **What is the output of the following code?**
console.log(1 == true);
console.log(0 == false);
- A) true, true
- B) true, false
- C) false, true
Answer: A) true, true*Answer: A) true, true**
12. **What is the purpose of the
bind() method in JavaScript?**- A) To bind a function to an object
- B) To bind an event to a function
- C) To bind a variable to a function
- D) To bind a Answer: A) To bind a function to an objectnd a fWhat will the following code output?the following code output?**
console.log(typeof undefined);
- A) "undefined"
- B) "null"
- C) "objeAnswer: A) "undefined" **AnWhich of the following is not a JavaScript framework or library?ript framework or library?**
- A) React
- B) Angular
- Answer: D) Djangoo
What is the output of the following code?put of the following code?**
console.log(2 + "2" - 1);
- A) "21"
- B) "22"
Answer: C) 21 D) 3
**Answer: C) 21**
16. **What is the purpose of the
map() method in JavaScript?**- A) To create a new array by applying a function to each element of an existing array
- B) To filter elements of an array based on a condition
- C) To reduce an array to a single value
- D) To sorAnswer: A) To create a new array by applying a function to each element of an existing arrayh elemWhat will the following code output?ill the following code output?**
console.log(typeof []);
- A) "array"
- B) "object"
- C) "Answer: B) "object"ll"
Which of the following is not a valid JavaScript loop?s not a valid JavaScript loop?**
- A)
for- B)
while- C)
do...while- D)
foreach**Answer: D)
foreach**👍1
19. What is the output of the following code?
- A) true
- B) false
- C) undefined
- D) null
Answer: B) false
20. **What is the purpose of the
- A) To handle asynchronous operations
- B) To handle synchronous operations
- C) To handle event listeners
- D) To handle DOM manipulation
Answer: A) To handle asynchronous operationsns**
These questions cover a range of JavaScript topics, including data types, operators, methods, and more, which are commonly asked in interviews.
console.log(1 === true);
- A) true
- B) false
- C) undefined
- D) null
Answer: B) false
20. **What is the purpose of the
Promise object in JavaScript?**- A) To handle asynchronous operations
- B) To handle synchronous operations
- C) To handle event listeners
- D) To handle DOM manipulation
Answer: A) To handle asynchronous operationsns**
These questions cover a range of JavaScript topics, including data types, operators, methods, and more, which are commonly asked in interviews.