Boost your coding efficiency by learning how to measure performance in JavaScript! π In this video, I explain how to use the console.time() method to track the execution time of your code.
Whether youβre optimizing loops, debugging slow processes, or just curious about performance, this simple yet powerful tool is a must-know for every developer. Watch now to level up your JavaScript skills! π»β¨"
Whether youβre optimizing loops, debugging slow processes, or just curious about performance, this simple yet powerful tool is a must-know for every developer. Watch now to level up your JavaScript skills! π»β¨"
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