Conditional Statments Flashcards
What is a conditional statement in JavaScript?
A conditional statement is a programming construct that evaluates a condition and executes code based on whether the condition is true or false.
What is the syntax for an if statement in JavaScript?
The syntax for an if statement is: if (condition) { // code to execute if condition is true }
True or False: The else statement is optional in an if-else structure.
True
Fill in the blank: The _____ statement allows you to specify a block of code to execute if the condition is false.
else
What is the syntax for an if-else statement in JavaScript?
The syntax is: if (condition) { // code if true } else { // code if false }
What is the purpose of the else if statement?
The else if statement allows you to test multiple conditions sequentially.
What is the syntax for an if-else if-else statement?
The syntax is: if (condition1) { // code if condition1 is true } else if (condition2) { // code if condition2 is true } else { // code if both conditions are false }
True or False: You can nest if statements inside other if statements.
True
What is the switch statement used for in JavaScript?
The switch statement is used to execute one block of code among multiple options based on the value of an expression.
What is the basic syntax of a switch statement?
The syntax is: switch (expression) { case value1: // code; break; case value2: // code; break; default: // code; }
In a switch statement, what does the break keyword do?
The break keyword exits the switch block and prevents the execution of subsequent cases.
What happens if you omit the break statement in a switch case?
If you omit the break statement, JavaScript will continue executing subsequent cases until it encounters a break or reaches the end of the switch block.
What is a ternary operator in JavaScript?
The ternary operator is a shorthand for an if-else statement, using the syntax: condition ? exprIfTrue : exprIfFalse.
Fill in the blank: The syntax of a ternary operator includes _____, _____, and _____.
condition, exprIfTrue, exprIfFalse
What is the purpose of the logical operators (&&, ||, !) in conditional statements?
Logical operators are used to combine multiple conditions in conditional statements.