What is the purpose of the 'else if' statement in JavaScript?
Explanation:
The 'else if' statement allows you to check multiple conditions sequentially. If the preceding 'if' or 'else if' conditions are false, the code block associated with the first true 'else if' condition is executed.
What is the value of 'x' after the following code is executed?
let x = 5;
if (x > 10) {
x = 10;
} else {
x = 0;
}
Explanation:
Since 'x' is initially 5, the condition 'x > 10' is false. Therefore, the code block under the 'else' statement is executed, setting 'x' to 0.
What is the purpose of the typeof
operator in JavaScript?
Explanation:
The typeof
operator returns a string representing the data type of its operand, such as 'string', 'number', 'boolean', 'object', 'undefined', etc. It's useful for understanding the nature of variables and expressions in your code.
Which data type represents a sequence of characters in JavaScript?
Explanation:
In JavaScript, the 'string' data type is specifically designed to store and manipulate textual data, representing sequences of characters.
Which statement can be used to exit a 'switch' statement?
Explanation:
The 'break' statement is crucial in a 'switch' statement. Without it, the code execution would fall through to the next case even if the current case matches. 'break' ensures that only the matching case's code is executed.
Which method is NOT a built-in JavaScript array method?
Explanation:
length
is a property of arrays, not a method. You access it as array.length
, not array.length()
.
How can you access the error message from a caught error?
Explanation:
Caught errors in JavaScript are typically Error objects. The error.message
property provides a descriptive message about the error.
How do you access the value associated with the key 'name' in the object person = {name: 'John', age: 30}
?
Explanation:
You can access object properties using dot notation (object.property) or bracket notation (object['property']).
What will be the output of the following code?
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
Explanation:
The 'while' loop will continue to execute as long as the condition 'i < 5' is true. In each iteration, the value of 'i' is printed and then incremented by 1. The loop stops when 'i' becomes 5.
How do you access the value entered into a text input field in a form?
Explanation:
The value
property of an input element (like a text field) stores the current value entered by the user.