if, else if, and else statements
comparison operators
logical operators
truthy vs falsy values
ternary operators
switch statement
[if]
In programming, we can also perform a task based on a condition using an if statement:
구조 :
if (true) {
console.log('This message will print!');
}
괄호(condition) + 코드 블럭{}
Let 과 var의 차이는 뭘까?
Let 은 true와 false값 지정이 가능한 것?
[comparison operators]
Less than: <
Greater than: >
Less than or equal to: <=
Greater than or equal to: >=
Is equal to: ===
Is not equal to: !==
let hungerLevel = 7;
if (hungerLevel > 7 )
{
console.log('Time to eat!');
}
else {
console.log('We can eat later!');
}
&& operator
Boolean = true, false
불리언 값과 함께 쓰이는 오퍼레이터
There are three logical operators:
the and operator (&&)
the or operator (||)
the not operator, otherwise known as the bang operator (!)
if (stopLight === 'green' && pedestrians === 0) {
console.log('Go!');
} else {
console.log('Stop');
}
if (day === 'Saturday' || day === 'Sunday') {
console.log('Enjoy the weekend!');
} else {
console.log('Do some work.');
}
let excited = true;
console.log(!excited); // Prints false
let sleepy = false;
console.log(!sleepy); // Prints true
let myVariable = 'I Exist!';
Check if the variable has been assigned a value.
if (myVariable) {
console.log(myVariable)
} else {
console.log('The variable does not exist.')
}
<—!false value—>
0
Empty strings like "" or ''
null which represent when there is no value at all
undefined which represent when a declared variable lacks a value
NaN, or Not a Number
let numberOfApples = 0;
if (numberOfApples){
console.log('Let us eat apples!');
} else {
console.log('No apples left!');
}
// Prints 'No apples left!'
[short-circuit evaluation - 단락 평가]
let tool = '';
// Use short circuit evaluation to assign writingUtensil variable below:
let writingUtensil = tool || 'pen'
console.log(The ${writingUtensil} is mightier than the sword.
);
->
let tool = 'marker';
// Use short circuit evaluation to assign writingUtensil variable below:
let writingUtensil = tool || 'pen'
console.log(The ${writingUtensil} is mightier than the sword.
);
[tenary operator]
let isNightTime = true;
if (isNightTime) {
console.log('Turn on the lights!');
} else {
console.log('Turn off the lights!');
}
isNightTime ? console.log('Turn on the lights!') : console.log('Turn off the lights!');
Ex2)
const plantNeedsWater = day =>
day === 'Wednesday' ? true : false;
const plantNeedsWater = day =>
day === 'Wednesday' ? true : false;
[else if statement]
//예제
let stopLight = 'yellow';
if (stopLight === 'red') {
console.log('Stop!');
} else if (stopLight === 'yellow') {
console.log('Slow down.');
} else if (stopLight === 'green') {
console.log('Go!');
} else {
console.log('Caution, unknown!');
}
A [switch statement] provides an alternative syntax that is easier to read and write. A switch statement looks like this:
구조 :
Switch (변수) {
Condition-
Case ‘value’ :
Console.log(‘…’);
Vreak;
Case ‘value’ :
Console.log(‘…’);
qksqhr.
Default:
Console.log(‘Invalid item’);
Break;
}
//예제
let groceryItem = 'papaya';
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;
}
// Prints 'Papayas are $1.29'