Application logic in JavaScript Flashcards
What is an assertion?
An assertion is a statement that evaluates to either true or false. For example:
true === true
or
1 <= 5
Logical assertions allow us to write conditional logic into our programs. We can tell our code to carry out one set of instructions if our assertion is true, and another if it is false.
When is a value false in Boolean?
// values that evaluate to false
Boolean(false); Boolean(""); // empty string Boolean(0); Boolean(null); Boolean(undefined); Boolean(NaN);
What are logical operators?
Logical operators are used to make assertions about 2 or more statements or values.
What does the two ampersand logical operator do?
Used to assert whether or not 2 statements are true. If you run true, two ampersands, and then false in a JavaScript console, you’ll see that you get false.
Two ampersands evaluate to true if the values on both sides of the operator evaluate to true.
What are the 3 logical operators?
Two ampersands (and)
|| (or)
! (not)
When does the || logical operator evaluate to true?
|| evaluates to true if one of the values evaluate to true.
When does the ! logical operator evaluate to true?
! negates a boolean value so !true evaluates to false, and !false evaluates to true.
What is the strict equality operator?
===
The strict equality operator === first compares data type of the 2 items being compared, and if they’re not the same data type, it returns false. If they are the same type, it then checks to see if they have the same value. The strict equality operator is called strict because it checks both sides of the comparison.
So, if you run true === 1, you’ll see that this evaluates to false.
What is the coerce equality operator?
==
The == operator in JavaScript has a looser notion of equality. When it compares 2 items, if it finds that the 2 values are not of the same type, it coerces (or converts) one of the value types to the type of the other.
So true == 1 evaluates to true because when you have a boolean and a number, the number gets converted to a boolean, and Boolean(1) is true.
What is control flow?
Control flow dictates how programs execute different sets of instructions based on differing conditions. You might have one branch that executes if a condition is true, and another that executes if it’s false. That’s control flow.
What are two ways to to achieve control flow?
conditionals (if, else, else if)
and
try/catch/finally blocks
What are the three keywords for conditionals?
if, else, else if
How do you create a conditional?
To use a conditional, you begin with the command (if in this case), followed by an expression wrapped in parentheses (true === true). Between the curly brackets ({…}) comes the statement(s) to be executed if the expression evaluates to true.
function sanityCheck() { if (true === true) { console.log("true is still true. that's reassuring"); } }
The same syntax is used for else and else if, but these both have the additional requirement that they must come after an adjacent if block.
function analyzeNumber(num) { if (typeof num !== 'number') { console.log('not a number'); } else if (num % 2 === 0) { console.log('even number'); } else { console.log('odd number'); } }
What is a ternary operator?
It’s a shortcut for the if conditional. The syntax for a ternary operator is a logical expression (or something that can be coerced to a boolean) followed by the ? operator, followed by the value to be returned if the condition is true, followed by the : operator, and finally the value to be returned if the condition is not true.
let myVar = condition1 ? ‘something other than default’ : null;
When dealing with conditional logic in the case of errors, what are the 3 commands you can use?
try/catch/finally
These language constructs allow us to specify a block of behavior that is to be tried (the try block). If that does not succeed, the behavior in the catch block runs. And in either the success or failure case, the instructions in the finally block will run. Note that try and catch can be used without finally.