JavaScript Conditionals Flashcards

(20 cards)

1
Q

What is a conditional statement?

A

A conditional statement checks a specific condition(s) and performs a task based on the condition(s).

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

How is an if statement composed?

A

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!’);
}

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

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.

A

By default an if statement checks weather a condition is true.

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

How is an if…else statement composed?

Follows on from the if statement.

A

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.’);
}

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

What is a binary decision?

A

A solution to a yes or no question. Where an if….else statement is used for.

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

Comparison Operators ‘is equal to’ and ‘is not equal to’ values are?

A

Equal to == is to compare the value only
Equal to === (Value and Data Type)
Is not Equal to !==

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

Name the three logical operators and the characters that represent them?

A

AND = &&
OR = ||
NOT = !

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

Describe how the not operator works?

A

When true the operator passes back false and when false it passes back true.

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

Describe the JavaScript logic in processing a if….else statement.

A

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.

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

What are Truthy and Falsy statements used for?

A

To check whether a variable has been assigned a value.

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

Can you perform an action by checking if a variable has a value?

A

Yes using a Truthy and Falsy statement.

let wordCount = 0;
if (wordCount) {
console.log(“Great! You’ve started your work!”);

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

Name the Falsy values?

A

0
Empty strings like “” or ‘’
null
undefined
NaN, or Not a Number

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

How is a Truthy and Falsy statement written using a shorthand.

A

By combining it with a logical operator.

let username = ‘’;
let defaultName = username || ‘Stranger’;
console.log(defaultName)

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 shorthand for an if…else statement.

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

How is Ternary Operator statement written?

A

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.

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

What is a else…if statement

A

An extension to the if…else statement. The else…if statement allows you to have more that two options\outcomes.

17
Q

How is a else…if statement written?

A

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.’);
}

18
Q

How does a switch statement differ from an else…if statement?

A

A switch statement is easier to read and write.
Done is a single block and not multiple blocks.

19
Q

How is a switch statement written?

A

switch (VALUE TO BE COMPARED ) {
case ‘expression’:
console.log(‘Hello’);
break;
case ‘expression1’:
console.log(‘Bye’);
break;
default:
console.log(‘Not here’);
break;
}

20
Q

What do the default and break keyword\statement do within in a switch statement?

A

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.