Application logic in JavaScript Flashcards

1
Q

What is an assertion?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When is a value false in Boolean?

A

// values that evaluate to false

Boolean(false);
Boolean(""); // empty string
Boolean(0);
Boolean(null);
Boolean(undefined);
Boolean(NaN);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are logical operators?

A

Logical operators are used to make assertions about 2 or more statements or values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the two ampersand logical operator do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the 3 logical operators?

A

Two ampersands (and)
|| (or)
! (not)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When does the || logical operator evaluate to true?

A

|| evaluates to true if one of the values evaluate to true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When does the ! logical operator evaluate to true?

A

! negates a boolean value so !true evaluates to false, and !false evaluates to true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the strict equality operator?

A

===

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the coerce equality operator?

A

==

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is control flow?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are two ways to to achieve control flow?

A

conditionals (if, else, else if)

and

try/catch/finally blocks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the three keywords for conditionals?

A

if, else, else if

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you create a conditional?

A

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');
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a ternary operator?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When dealing with conditional logic in the case of errors, what are the 3 commands you can use?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the (e) or (err) in catch (e) or catch (err) mean?

A

The e or err that you see in catch (e) or catch (err) will be an object representing the error. The error object will have information about the error that occurred. Stick to e or err since those are the conventions.