What is a conditional statement?
A conditional statement checks a specific condition(s) and performs a task based on the condition(s).
How is an if statement composed?
if word, the condition in parentheses () , the code block in curly braces {}.
The condition could be a variable (Example below).
let sale = true;
if (sale) {
console.log(‘Time to buy!’);
}
Which if the following sentences is correct.
By default an if statement checks weather a condition is true.
By default an if statement checks weather a condition is false.
By default an if statement checks weather a condition is true.
How is an if…else statement composed?
Follows on from the if statement.
Directly at the end of the if code block add else with a new set of curly braces to contain the new code block.
if(sale) {
console.log(‘Time to buy!’);
} else {
console.log(‘Time to wait for a sale.’);
}
What is a binary decision?
A solution to a yes or no question. Where an if….else statement is used for.
Comparison Operators ‘is equal to’ and ‘is not equal to’ values are?
Equal to == is to compare the value only
Equal to === (Value and Data Type)
Is not Equal to !==
Name the three logical operators and the characters that represent them?
AND = &&
OR = ||
NOT = !
Describe how the not operator works?
When true the operator passes back false and when false it passes back true.
Describe the JavaScript logic in processing a if….else statement.
JavaScript processes from left to right and then top to bottom. Example when using the || (or) logical operator the first condition that is true will execute the code and the remaining conditions ignored.
When the if statement is true the else statement is ignored.
What are Truthy and Falsy statements used for?
To check whether a variable has been assigned a value.
Can you perform an action by checking if a variable has a value?
Yes using a Truthy and Falsy statement.
let wordCount = 0;
if (wordCount) {
console.log(“Great! You’ve started your work!”);
Name the Falsy values?
0
Empty strings like “” or ‘’
null
undefined
NaN, or Not a Number
How is a Truthy and Falsy statement written using a shorthand.
By combining it with a logical operator.
let username = ‘’;
let defaultName = username || ‘Stranger’;
console.log(defaultName)
What is a Ternary Operator?
It’s a shorthand for an if…else statement.
How is Ternary Operator statement written?
Condition ? True Expression : False Expression;
isNightTime ? console.log(‘Turn on the lights!’) : console.log(‘Turn off the lights!’);
If the condition is True then the first expression is executed else the false expression.
What is a else…if statement
An extension to the if…else statement. The else…if statement allows you to have more that two options\outcomes.
How is a else…if statement written?
There is if statement, a else if statement and then finally a else statement.
They a written like if…else statement but with the extra blocks.
if (season === ‘spring’) {
console.log(‘1’);
} else if (season === ‘winter’) {
console.log(‘2’);
} else {
console.log(‘Invalid season.’);
}
How does a switch statement differ from an else…if statement?
A switch statement is easier to read and write.
Done is a single block and not multiple blocks.
How is a switch statement written?
switch (VALUE TO BE COMPARED ) {
case ‘expression’:
console.log(‘Hello’);
break;
case ‘expression1’:
console.log(‘Bye’);
break;
default:
console.log(‘Not here’);
break;
}
What do the default and break keyword\statement do within in a switch statement?
default - Each switch statement needs a default value as backup incase the above cases aren’t true.
break - Without the break keyword the computer will carry on checking each case and not exit the block once a case has matched.