Control Flow Flashcards
(40 cards)
What is flow control?
When writing programs, you want your data to take the correct path. You want it to turn left or right, up, down, reverse, or proceed straight ahead when it’s supposed to.
We call this flow control.
What is a conditional?
A conditional is a fork (or multiple forks) in the road.
They use the keywords ‘if’ and ‘else’
What is a clause in this context?
A block, statement, or expression
What are operands?
The expressions or values that an operator uses are its operands. In comparisons, the expressions to the left and right of the operator are the operands. For instance, the equality comparison x === y uses the === operator with two operands: x and y.
What is this ‘===’ operator called?
The strict equality operator, also known as the identity operator, returns true when the operands have the same type and value, false otherwise.
What is this ‘!==’ operator called?
The strict inequality operator returns false when the operands have the same type and value, true otherwise. Note that !== is the inverse of ===: when === returns true, !== returns false, and vice versa.
What is this ‘==’ operator called?
The non-strict equality operator, also known as the loose equality operator, is similar to ===.
However, when the operands have different types, == attempts to coerce one of the operands to the other operand’s type before it compares them, and it may coerce both operands in some cases.
The result is true when the final values are the same, false otherwise. The coercion behavior can lead to unexpected results. For instance, when we compare the number 5 to the string ‘5’ using ==, we get true; with ===, we get false.
What is this ‘!=’ operator called?
The non-strict inequality operator, also known as the loose inequality operator, is similar to !==. However, when the operands have different types, != attempts to coerce one of the operands to the other operand’s type before it compares them, and it may coerce both operands in some cases. The result is false when the final values are the same, true otherwise.
What is this ‘<’ operator called?
The less than operator returns true when the value of the left operand has a value that is less than the value of the right operand, false otherwise.
What is happening here?
When comparing strings, the comparison is character-by-character. JavaScript moves from left-to-right in the strings looking for the first character that is different from its counterpart in the other string. Once it finds a character that differs, it compares that character with its counterpart, and makes a decision based on that. If both strings are equal up to the length of the shorter string as in the next to last example, then the shorter string is considered less than the longer string.
The final example shows that if you use < with two different types, some sort of coercion will take place. In this case, “42” gets coerced to a number, so a numeric comparison takes place. Don’t try to remember this.
What is this ‘>’ operator called?
The greater than operator returns true when the value of the left operand has a value that is greater than the value of the right operand, false otherwise.
What is this ‘<=’ operator called?
The less than or equal to operator returns true when the value of the left operand has a value that is less than or equal to the value of the right operand, false otherwise. Note that =< is not a valid comparison operator.
What is this ‘>=’ operator called?
The greater than or equal to operator returns true when the value of the left operand has a value that is greater than or equal to the value of the right operand, false otherwise. Note that => is not a valid comparison operator.
What is this ‘!’ operator called?
The not operator returns true when its operand is false and returns false when the operand is true. That is, it negates its operand. Note that, unlike most operators, ! takes a single operand; the operand appears to the right of the operator.
What is the ‘&&’ operator called?
The ‘and’ operator returns true when both operands are true and false when either operand is false.
What is the ‘||’ operator called?
The or operator returns true when either operand is true and false when both operands are false.
What is short circuit evaluation?
Consider these two expressions:
The first expression returns true when item is both red and portable. If either condition is false, then the overall result must be false. Thus, if the program determines that item is not red, it doesn’t have to check whether it is portable. JavaScript short-circuits the entire expression by terminating evaluation as soon as it determines that item isn’t red. It doesn’t need to call isPortable() since it already knows that the entire expression must be false.
Why would it print ‘how can this be true’?
The first example logs “how can this be true?” while the second logs “it is not true.” This works since JavaScript coerces the value 5 to true, and the value 0 to false. To repeat, JavaScript can coerce any value to a boolean. Thus, you can use any expression in a conditional expression. We often say that the expression evaluates as or evaluates to true or false.
What would be printed here?
The first example logs “how can this be true?” while the second logs “it is not true.” This works since JavaScript coerces the value 5 to true, and the value 0 to false. To repeat, JavaScript can coerce any value to a boolean. Thus, you can use any expression in a conditional expression. We often say that the expression evaluates as or evaluates to true or false.
What are the JS versions of ‘false’?
-false
-The number 0. This includes all 3 variations of zero in JavaScript:
0: The ordinary zero value.
-0: A negative zero. That’s mathematical nonsense, but a real thing in JavaScript.
0n: The BigInt version of zero.
An empty string (‘’)
undefined
null
NaN
What is truthy and falsy?
We often use the term falsy to refer to values that evaluate as false, while the values that evaluate as true are truthy. We use these terms when we need to distinguish between boolean true and false values. We can also discuss truthiness: whether something is a truthy or falsy value.
What is the return value when using ‘&&’ and ‘||’ operators?
They use short circuit evaluation.
The return value will be the last evaluated operand.
What is happening here?
In reality, !! isn’t a separate operator in JavaScript. Instead, it’s two consecutive ! operators. The expression !!a is equivalent to writing !(!a). The inner ! converts the value of a to false if it is truthy, or true if a is falsy. The outer ! then flips true to false or false to true. In the end, we end up with a boolean value instead of a truthiness value:
What is this ‘??’ operator called?
The nullish coalescing operator evaluates to the right-hand operand if the left-hand operand is nullish (either null or undefined). Otherwise, it evaluates to the value of the left-hand operand.