Flow Control Flashcards
What is the return value of a && or || comparison?
When using&&and||, the return value is always the value of the operand evaluated last: This is usually a truthy or falsy value.
In && and || comparisons, are both sides always evaluated?
In a && comparison, if the program determines thatitem1is not true, it doesn’t have to check whether item2 is true. Likewise, in a || comparison, if item1 is true, the program does not need to check item2.
What is the syntax for the ternary operator?
let isOk = (foo || bar) ? true : false;
How can we use short circuiting to prevent an error from being thrown?
if (name && name.length > 0) {
console.log(Hi, ${name}.
);
} else {
console.log(“Hello, whoever you are.”);
}
What is the nullish coalescing operator?
Thenullish coalescing operatorevaluates to the right-hand operand if the left-hand operand isnullish(eithernullorundefined). Otherwise, it evaluates to the value of the left-hand operand.
What is the syntax for the nullish coalescing operator?
??
function foo(str) {
let found = [“Pete”, “Alli”, “Chris”].find(name => name === str);
return found ?? “Not found”;
}
When should you use the ternary operator?
Ternary expressions should usually be used to select between 2 values, not to choose between two actions.
Can you treat a ternary operator like a value?
the entire structure is an expression so we can treat it like a value.
what is the order of operator precedence?
The following is a list of the comparison operations from the highest precedence (top) to lowest (bottom).
-
<=
,<
,>
,>=
- Comparison -
===
,!==
,==
,!=
- Equality -
&&
- Logical AND -
||
- Logical OR
What is the difference between the pre-increment operator ++i and the post increment operator i++?
pre-increment form returns the new value of the variable, while the post-increment form returns the previous value of the variable.
What are the keywords you can use to interrupt a loop? How are they different?
JavaScript uses the keywordscontinueandbreakto provide more control over loops.continuelets you start a new iteration of the loop, whilebreaklets you terminate a loop early.