Truthy and Falsy Assignment
Truthy and Falsy Assignment
Say you have a website and want to take a user’s username to make a
personalized greeting. Sometimes, the user does not have an account,
making the username variable falsy. The code below checks if username is
defined and assigns a default string if it is not:
if (username) {
defaultName = username;
} else {
defaultName = 'Stranger';
}
Instructions
1.
Let’s use short-circuit evaluation to assign a value to writingUtensil. Do
not edit tool yet, we’ll return to tool in the next step.
Concept Review
let tool = '';
tool = 'marker';
// Use short circuit evaluation to assign writingUtensil variable below:
let writingUtensil = tool || 'pen'
console.log(writingUtensil);
console.log(`The ${writingUtensil} is mightier than the sword.`);
CONDITIONAL STATEMENTS
Ternary Operator
if (isNightTime) {
console.log('Turn on the lights!');
} else {
console.log('Turn off the lights!');
}
We can use a ternary operator to perform the same functionality:
isNightTime ? console.log('Turn on the
lights!') : console.log('Turn off the lights!');
In the example above:
Instructions
1.
2.
3.
Concept Review
Want to quickly review some of the concepts you’ve been learning? Take
a look at this material's cheatsheet!
Community Forums
let isLocked = false;
isLocked ? console.log('You will need a key to open the door.') : console.lo
g('You will not need a key to open the door.');
let isCorrect = true;
isCorrect ? console.log('Correct!') : console.log('Incorrect!');
let favoritePhrase = 'Love That!';
favoritePhrase === 'Love That!' ? console.log('I love that!') : console.log(
"I don't love that!");
CONDITIONAL STATEMENTS
Else If Statements
Instructions
1.
Let’s create a program that keeps track of the way the environment
changes with the seasons. Write a conditional statement to make this
happen!
2.
Inside the code block of the else if statement you just created, add
a console.log() that prints the string 'It\'s fall! Leaves are falling!'.
Checkpoint 3 Passed
Inside the code block of the else if statement you just created, add
a console.log() that prints the string 'It\'s sunny and warm because it\'s
summer!'.
Checkpoint 4 Passed
Concept Review
Want to quickly review some of the concepts you’ve been learning? Take
a look at this material's cheatsheet!
Community Forums
let season = 'summer';
if (season === 'spring') {
console.log('It\'s spring! The trees are budding!');
} else if (season === 'winter') {
console.log('It\'s winter! Everything is covered in snow.');
} else if (season === "fall") {
console.log('It\'s fall! Leaves are falling!');
} else if (season === 'summer') {
console.log('It\'s sunny and warm because it\'s summer!');
} else {
console.log('Invalid season.');
}
CONDITIONAL STATEMENTS
The switch keyword
else if statements are a great tool if we need to check multiple
conditions. In programming, we often find ourselves needing to check
multiple values and handling each of them differently. For example:
switch (groceryItem) {
case 'tomato':
console.log('Tomatoes are $0.49');
break;
case 'lime':
console.log('Limes are $1.49');
break;
case 'papaya':
console.log('Papayas are $1.29');
break;
default:
console.log('Invalid item');
break;
}
Instructions
1.
Let’s write a switch statement to decide what medal to award an athlete.
Concept Review
Want to quickly review some of the concepts you’ve been learning? Take
a look at this material's cheatsheet!
Community Forums
let athleteFinalPosition = 'first place';
switch (athleteFinalPosition) {
case 'first place':
console.log('You get the gold medal!');
break;
case 'second place':
console.log('You get the silver medal!');
break;
case 'third place':
console.log('You get the bronze medal!');
break;
default:
console.log('No medal awarded.');
break;
}
CONDITIONAL STATEMENTS
Review
Way to go! Here are some of the major concepts for conditionals:
Instructions