Conditional Logic in JavaScript Flashcards

(32 cards)

1
Q

What is Conditional Logic good for?

A

It’s good for executing a set of instructions in one scenario but to do something else in another.

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

What does an If Statement check?

A

An if Statement will check if an expression evaluates to true, and if it does, a subsequent code block will be executed.

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

What is a Code Block?

A

A Code Block is a group of statements contained in a pair of curly braces {}

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

What is a Condition?

A

An Expression used by an If Statement to evaluate whether it’s TRUE or FALSE

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

How is an If Statement Constructed?

A

if (condition) { // code block to be executed if condition is true
}

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

What part of the if statement is wrapped in curly braces?

A

The code block to be executed

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

What is a Boolean Context?

A

A Boolean Context is any situation where JavaScript uses a Condition to decide whether or not to execute a certain code

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

What are the other values, aside from true, that will evaluate to true in a boolean context?

A

any string except an empty string, any number other than zero, objects and arrays.

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

What are the truthy values?

A

true itself, any strings except an empty string, any number except zero, objects and arrays.

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

What is the name for all values that are not truthy?

A

Falsy values.

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

What will falsy values evaluate to in a boolean context?

A

False

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

What are the 6 falsy values?

A

NaN, Null, Undefined, an empty string, 0, false.

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

How many falsy values are there in JavaScript?

A

6

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

What does an empty string look like?

A

” “

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

What is Null Usually assigned to a variable for?

A

To convey the absence of data

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

What can an If-Else Statement be thought of?

A

As a fork in the road in our code. If we can’t go down one path, we can go down another.

16
Q

Which code block is executed when the if condition evaluates to false?

A

The else code block will be executed.

17
Q

What happens if we chain if statements?

A

By chaining if statements, we can create as many forks in our code as we wish.

18
Q

What are Ternary Statements an alternative way of doing?

A

Ternary Statements are an alternative way of using an expression in a boolean context that can make code more concise in certain situations.

19
Q

Why would this code resolve to a Reference Error?
const isVirtualPetHappy = true;

if (isVirtualPetHappy) {
let action = ‘wag tail’
} else {
let action = ‘whimper’
};

console.log(action)

A

The code would resolve to a Reference Error because the action inside console.log(action) has been declared inside a code block {} which makes it unavailable later on in the code.

20
Q

How would you correct The Reference Error?

A

By Declaring the Variable before the If Statement but waiting to Initialise It.

21
Q

Why would this code correctly run instead?
const isVirtualPetHappy = true;
let action;

if (isVirtualPetHappy) {
action = ‘wag tail’
} else {
action = ‘whimper’
};

console.log(action)

A

The code will run because the variable (action) in console.log(action) has been declared before the if Statement but hasn’t been initialised =’wag tail’ or ‘whimper’ until after the if statement.

22
Q

Why are these two codes the equivalent of one another?
const action = isVirtualPetHappy ? ‘wag tail’ : ‘whimper’;
+
const isVirtualPetHappy = true;
let action;
if (isVirtualPetHappy) {
action = ‘wag tail’
} else {
action = ‘whimper’
};

console.log(action)

A

One is a Ternary Statement.

23
Q

What is the symbol used for the Ternary Statement?

24
How does this code work? const action = isVirtualPetHappy ? 'wag tail' : 'whimper';
The constant variable is declared action and we assign the ternary operator by placing it's symbol (?) after the if statement. If the value of the if statement is truthy, then it will evaluate to the first expression 'wag tail'. If the value of the statement if falsy, then it will evaluate to the second expression 'whimper.'
25
What 3 parts can a Ternary Statement be broken down into?
condition ? expressionIfTrue : expressionIfFalse
26
What are Switch Statements?
Switch Statements allow us to execute code based on a set of conditions, known as cases.
27
What is Switch Statements a Clearer Way of Doing?
Switch Statements are a clearer way of managing the flow of your code when you have several or more conditions.
28
What does a Switch Statement start with?
Switch Statements start with the keyword switch followed by a set of parentheses () that wrap an expression.
29
What follows the beginning of a Switch Statement?
It's followed by a code block containing cases - a set of conditions, one of which will match the expression.
30
What is the break keyword used for in a Switch Statement?
Each case may contain code to execute or it may fall through to the next case and if these two scenarios are not desired then a break keyword is used at the end of each case to stop any more code in the switch statement from executing.
31
What does it mean when the default case is executed?
The default cause is executed when none of the cases match.