Conditional Logic in JavaScript Flashcards
(32 cards)
What is Conditional Logic good for?
It’s good for executing a set of instructions in one scenario but to do something else in another.
What does an If Statement check?
An if Statement will check if an expression evaluates to true, and if it does, a subsequent code block will be executed.
What is a Code Block?
A Code Block is a group of statements contained in a pair of curly braces {}
What is a Condition?
An Expression used by an If Statement to evaluate whether it’s TRUE or FALSE
How is an If Statement Constructed?
if (condition) { // code block to be executed if condition is true
}
What part of the if statement is wrapped in curly braces?
The code block to be executed
What is a Boolean Context?
A Boolean Context is any situation where JavaScript uses a Condition to decide whether or not to execute a certain code
What are the other values, aside from true, that will evaluate to true in a boolean context?
any string except an empty string, any number other than zero, objects and arrays.
What are the truthy values?
true itself, any strings except an empty string, any number except zero, objects and arrays.
What is the name for all values that are not truthy?
Falsy values.
What will falsy values evaluate to in a boolean context?
False
What are the 6 falsy values?
NaN, Null, Undefined, an empty string, 0, false.
How many falsy values are there in JavaScript?
6
What does an empty string look like?
” “
What is Null Usually assigned to a variable for?
To convey the absence of data
What can an If-Else Statement be thought of?
As a fork in the road in our code. If we can’t go down one path, we can go down another.
Which code block is executed when the if condition evaluates to false?
The else code block will be executed.
What happens if we chain if statements?
By chaining if statements, we can create as many forks in our code as we wish.
What are Ternary Statements an alternative way of doing?
Ternary Statements are an alternative way of using an expression in a boolean context that can make code more concise in certain situations.
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)
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.
How would you correct The Reference Error?
By Declaring the Variable before the If Statement but waiting to Initialise It.
Why would this code correctly run instead?
const isVirtualPetHappy = true;
let action;
if (isVirtualPetHappy) {
action = ‘wag tail’
} else {
action = ‘whimper’
};
console.log(action)
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.
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)
One is a Ternary Statement.
What is the symbol used for the Ternary Statement?
?