Section 4: Control Structures - Conditional Code & Loops
The goals
๐ช๐ปConditional Statement (if statements) & Expressions
โ๐ปBoolean Values & Operators
๐๐ปLoops in JavaScript
๐๐ปError Handling
Conditional Code Execution
function doSomething() - if (someCondition)
Option A:
e.g. agg two numbers
Option B:
e.g. subtract two numbers
Boolean Operators
Important For Conditional Code: Return true or false
==: Check for value equality
e.g. a == b
!=: Check for value inequality
e.g. a!= b
=== and !==: Check for value AND type (in)equality
e.g. a === b / a !== b
We should prefer this over "=="
> & <: Check for value being greater / smaller
e.g. a > b / a < b
>= & <=: Check for value being greater or equal / smaller or equal
e.g. a >= b / a <= b
!: Check if NOT true
e.g. !a
ํ ์คํธ ๋น๊ต & ์กฐ๊ฑด์์ ๋ถ๋ฆฌ์ธ ์ฌ์ฉํ๊ธฐ
"์กฐ๊ฑด๋ฌธ" ์ดํดํ๊ธฐ
ํญ์ ์๋์ ์กฐ๊ฑด์
if (condition) { ... }
๋ถ๋ฆฌ์ธ ๊ฐ์ด์ด์ผ ํ๋ค๋ ์ ์ ํญ์ ๊ธฐ์ตํด ์ฃผ์ธ์.
์ข ์ข ===, >, < ๋ฑ์ผ๋ก ์ด๋ฌํ ๋ถ๊ฐ์ ์์ฑํฉ๋๋ค. ์ด ๋ชจ๋ ์ฐ์ฐ์๋ ๋ถ๊ฐ์ ์ฐ์ถํฉ๋๋ค(์ฌ์ฉ ์ค์ธ ๋ณ์/๊ฐ์ ๋ฐ๋์ง ์์ต๋๋ค).
if์๋ ๋ถ๋ฆฌ์ธ๋ง ๋ค์ด๊ฐ๋ฉด ๋๊ธฐ ๋๋ฌธ์ ์ด๋ฌํ ์ฐ์ฐ์๋ฅผ ๊ผญ ์ฌ์ฉํด์ผ ํ ํ์๋ ์์ต๋๋ค. ๋ถ๋ฆฌ์ธ์ ํฌํจํ๋ ๋ณ์๊ฐ ์์ผ๋ฉด ๋ณ๋์ ์ฐ์ฐ์ ์์ด ์ฌ์ฉํ ์ ์์ต๋๋ค.
์์:
const isLoggedIn = true;
if (isLoggedIn) {
// This code will execute because isLoggedIn is true => A valid condition
}
์๋์ ๊ฐ์ด ์ธ ์๋ ์์ง๋ง
const isLoggedIn = true;
if (isLoggedIn === true) {
...
}
๋ถํ์ํฉ๋๋ค. ์ด ์ฝ๋๋ ๋ถ๋ฆฌ์ธ์ด ์๋ ๊ณณ์์ ์๋ก์ด ๋ค๋ฅธ ๋ถ๋ฆฌ์ธ์ ์์ฑํ๊ฒ ๋ฉ๋๋ค.
! ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ ๋ถ์ ("๋ฐ์ ")ํ ์ ์์ต๋๋ค.
const isLoggedIn = true;
if (!isLoggedIn) {
// This code will NOT execute because isLoggedIn is true but ! inverts it (in this check)
} else {
// This would execute because !isLoggedIn yields false => else block executes
}
๋ค์์ ์ฝ๋์ ๋น์ทํฉ๋๋ค.
const isLoggedIn = true;
if (isLoggedIn !== true) {
// This would NOT execute
} else {
// This would execute because isLoggedIn is true and hence !== true yields false
}
ํ์ง๋ง ์ด ๋ํ ์ค๋ณต ์ฝ๋์ ํด๋นํฉ๋๋ค.
ํ ์คํธ(๋ฌธ์์ด) ๋น๊ต์ ๋ํ ์ถ๊ฐ ์ ๋ณด
๋ฌธ์์ด๋ ๋ณด๋ค ํผ(>) ๋๋ ๋ณด๋ค ์์(<) ์ฐ์ฐ์์ ๋น๊ตํ ์ ์์ต๋๋ค.
JavaScript๋ ์ ๋์ฝ๋ ๊ฐ์ ์ฌ์ฉํ์ฌ ํ์ค ์ฌ์ ์์ ๋ฐ๋ผ ๋ฌธ์์ด์ ๋น๊ตํฉ๋๋ค.
์๋ฅผ ๋ค์ด b๊ฐ a๋ณด๋ค ํฝ๋๋ค.
JavaScript๋ ํญ์ ์ฒซ ๋ฒ์งธ ๋ฌธ์๋ฅผ ๋ณด๊ณ , ์ฒซ ๋ฒ์งธ ๋ฌธ์๊ฐ ์ ์ฌํ ๊ฒฝ์ฐ์๋ง ๋ค๋ฅธ ๋ฌธ์๋ฅผ ๊ณ ๋ คํฉ๋๋ค. ๋ํ ๋๋ฌธ์๋ ์๋ฌธ์๋ณด๋ค ์์ ๊ฒ์ผ๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์๋์ ์์๋ ์ฐธ๊ณ ํด ์ฃผ์ธ์.
'ab' > 'aa' // true
'a' > 'B' // true
'a' > 'b' // false
Beware of Objects & Arrays in Comparisons!
Objects and arrays are kind of special in JavaScript!
We need to compare it to the specific item
Combining Conditions
Condition A And Condition B Or Condition C
&& ||
1. Evaulated together (yields true if each condition yields true)
2. Evaluated as an alternative
=> Yields true if Part 1 Or Part 2 yields true
You can use parentheses to control what's evaluated together
Operator Precedence
Operator precedence determines how operators are parsed concerning each other. Operators with higher precedence become the operands of operators with lower precedence.
a || (b * c); // evaluate `a` first, then produce `a` if `a` is "truthy"
a && (b < c); // evaluate `a` first, then produce `a` if `a` is "falsy"
a ?? (b || c); // evaluate `a` first, then produce `a` if `a` is not `null` and not `undefined`
a?.b.c; // evaluate `a` first, then produce `undefined` if `a` is `null` or `undefined`
Falsy and Truthy Values
Coercion vs Conversion
JavaScript์์๋ ๋น๊ต ์ฐ์ฐ์ ์์ด๋ ์กฐ๊ฑด๋ฌธ์์ ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค๋ ์ ์ ์ดํดํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
๋ค์๊ณผ ๊ฐ์ด ๋ถ๋ฆฌ์ธ ๋ณ์๋ฅผ ๊ณ ๋ คํ๋ ์๋ฅผ ๋ณด์๋ฉด, ์ด๋ ๋ถ๋ช ํฉ๋๋ค.
let isLoggedIn = true;
if (isLoggedIn) {
...
}
if๋ ์ฐธ์ด๋ ๊ฑฐ์ง์ ๋ฐํํ๋ ์กฐ๊ฑด์ ์ํ๊ธฐ ๋๋ฌธ์ ๋ถ๋ฆฌ์ธ ๋ณ์๋ ๋ถ๊ฐ์ ์ ๊ณตํ๊ธฐ๋ง ํ๋ฉด - ์ถ๊ฐ ๋น๊ต ์์ด ์๋ํฉ๋๋ค(if (isLoggedIn === true ) - ์๋์ ํ์ง๋ง ์ค๋ณต๋จ).
์์ ์๋ ์ดํดํ์ จ์ด๋ ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๋ฅผ ์ฒ์ ์ ํ๋ฉด ํผ๋์ค๋ฌ์ธ ์ ์์ต๋๋ค.
let userInput = 'Max';
if (userInput) {
... // this code here will execute because 'Max' is "truthy" (all strings but empty strings are)
}
JavaScript๋ if(๋๋ ์กฐ๊ฑด์ด ํ์ํ ๋ค๋ฅธ ์์น)์ ์ ๋ฌํ ๊ฐ์ ๋ถ๊ฐ์ผ๋ก ๊ฐ์ ๋ณํ("์ค์ ๋ก ๋ณํํ์ง ์๊ณ ๋ณํ")ํ๋ ค๊ณ ํฉ๋๋ค. ์ฆ 'Max' ๋ฅผ ๋ถ๋ฆฌ์ธ์ผ๋ก ํด์ํ๋ ค๊ณ ํ๊ณ ์ด์ ๊ฐ์์์ ์ค๋ช ํ ๊ท์น์ ๋ฐ๋ฆ ๋๋ค(์: 0 ๋ false๋ก ์ฒ๋ฆฌ๋๊ณ ๋ค๋ฅธ ๋ชจ๋ ์ซ์๋ true ๋ฑ์ผ๋ก ์ฒ๋ฆฌ๋ฉ๋๋ค.)
JavaScript๊ฐ ์ค์ ๋ก ๊ฐ์ ๋ณํํ์ง๋ ์๋๋ค๋ ๊ฒ์ ์ดํดํ์๋ ๊ฒ ์ค์ํฉ๋๋ค.
userInput ์ ์์ ๊ฐ์ ์กฐ๊ฑด์์ ์ฌ์ฉ๋ ํ์๋ ์ฌ์ ํ 'Max' ๋ฅผ ์ ์งํฉ๋๋ค - ์ฆ ๋ถ๋ฆฌ์ธ์ผ๋ก ๋ณํ๋์ง ์์ต๋๋ค. ๋ง์ผ ์ค์ ๋ก ๋ณํํ๋ค๋ฉด ๋ณ์์ ์ ์ฅ๋ ๊ฐ์ ์๊ฒ ๋๊ธฐ ๋๋ฌธ์ ๋์ฐํ ๊ฒ๋๋ค.
๋์ ์๋์ ๊ฐ์ ์ฝ๋๋
if (userInput) { ... }
๋ด๋ถ์ ์ผ๋ก ์๋์ ๊ฐ์ด ๋ณํ๋ฉ๋๋ค
if (userInput === true) {
๊ทธ๋ฆฌ๊ณ ์ฌ๊ธฐ์์ === ์ฐ์ฐ์ ๋ ๋ถ๋ฆฌ์ธ์ ์์ฑํ๊ณ ๋ฐํํฉ๋๋ค. ๋ํ ๋น๊ต ์ค์ธ ๋ณ์๋ฅผ ๊ฑด๋๋ฆฌ์ง ์์ต๋๋ค - userInput ์ ๋ฌธ์์ด๋ก ์ ์ง๋ฉ๋๋ค. ํ์ง๋ง ๋น๊ต์ ์ผ์์ ์ผ๋ก ์ฌ์ฉ๋๋ ์ ๋ถ๋ฆฌ์ธ์ ์์ฑํฉ๋๋ค.
์ด๊ฒ์ด ๋ฐ๋ก JavaScript๊ฐ ๋ค์๊ณผ ๊ฐ์ ํญ๋ชฉ์ ๋๋ฌํ์ ๋ ์๋์ผ๋ก ์ํํ๋ ์์ ์ ๋๋ค.
if (userInput) { ... }
"Boolean Tricks" with Logical Operators
Boolean Coercion via double NOT (double bang) operator
!!
e.g. !!"", !!1 -> false, true
Default value assignment via OR operator
||
e.g. const name = someInput || 'Max' -> SomeInput if not falsy, 'Max' otherwise
Use value if condition is true via AND operator
&&
e.g. const name = isLoggedIn && 'Max' -> 'Max' is set if isLoggedIn is true, false otherwise
const userInput = "";
const userName = userInput || 'Max';
userName // "Max"
Because userInput is falsy value so it will choose 'Max' as its value.
const realUserInput = 'Manu';
const realUserName = realUserInput || 'Max';
realUserName // 'manu'
Because it returns the first true or truthy value which is the realUserInput here
๋ค์์ ๋ค์ ๋ณต์ต, ํน์ ์ถ๋ ฅ์ ์ํ ์ฐธ๊ณ ์๋ฃ๋ก JavaScript์์ ๋ ผ๋ฆฌ ์ฐ์ฐ์์ ๋น๊ต ์ฐ์ฐ์๊ฐ ์ด๋ป๊ฒ ์๋ํ๋์ง์ ๋ํ ๊ฐ๋จํ ์์ฝ์ ๋๋ค.
const userName = 'Max';
const altName = '';
console.log(userName === 'Max'); // ๋ถ๋ฆฌ์ธ ๊ฐ ์ฐธ์ ์์ฑํ๊ณ ์ถ๋ ฅ
console.log(userName); // ๋ฌธ์์ด 'Max'์์ ๋ณ๊ฒฝ๋์ง ์์
console.log(userName || null); // userName์ด Truthy์ด๋ฏ๋ก ||๋ก 'Max'๊ฐ ๋ฐํ๋จ
console.log(altName || 'Max'); // altName์ (๋น ๋ฌธ์์ด์ด๋ฏ๋ก)Falsy์ด๋ฉฐ 'Max'๊ฐ ๋ฐํ๋จ
console.log(altName || ''); // altName๊ณผ ''๋ ๋ชจ๋ Falsy์ด์ง๋ง ๋ง์ผ ์ฒซ ํผ์ฐ์ฐ์๊ฐ Falsy๋ผ๋ฉด ํญ์ ๋ ๋ฒ์งธ ํผ์ฐ์ฐ์๊ฐ ๋ฐํ๋๋ฏ๋ก ''์ด ๋ฐํ๋จ
console.log(altName || null || 'Anna'); // altName๊ณผ null์ Falsy์ด๋ฏ๋ก 'Anna'๊ฐ ๋ฐํ๋จ
console.log(userName && 'Anna'); // userName์ Truthy์ด๋ฏ๋ก ๋ ๋ฒ์งธ (!) ๊ฐ์ธ 'Anna'๊ฐ ๋ฐํ๋จ
console.log(altName && 'Anna'); // altName์ Falsy์ด๋ฏ๋ก ์ฒซ ๋ฒ์งธ ๊ฐ์ธ ''์ด ๋ฐํ๋จ
console.log(userName && ''); // userName์ Truthy์ด๋ฏ๋ก ๋ ๋ฒ์งธ ๊ฐ์ธ ''์ด ๋ฐํ๋จ
ํญ์ ๋ช ์ฌํ์ธ์. ๊ทธ ์ด๋ค ์ฐ์ฐ์ (===, > ๋ฑ์ด๋ && ํน์ ||)๋ ๋น๊ต์ ์ฐ์ด๋ ๋ณ์๋ฅผ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค.
์์ ์์์ userName ๊ณผ altName์ ์ ์ฅ๋ ๊ฐ์ ์ ๋ ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค.
===, >๋ฑ์ ๋จ์ง ๋น๊ต์ ์ฐ์ด๋ ๋ถ๊ฐ๋ง ์์ฑํฉ๋๋ค. || ์ && ๋ ๋ถ๋ฆฌ์ธ์ ์์ฑํ๋ ๊ฒ์ด ์๋๋ผ, ์ฐ์ฐ์ ์ ํ์ ์๋ ๊ฐ์ ์กฐ๊ฑด์ผ๋ก ์ทจ๊ธํฉ๋๋ค (๋ฐ๋ผ์ ๋ถ๊ฐ์ ์ฐ์ถํ๊ณ ํ์ํ ๊ฒฝ์ฐ ๋ถ๋ฆฌ์ธ์ผ๋ก ๊ฐ์ ๋ณํ๋ฉ๋๋ค).
์์์ ์ค๋ช ํ ์๋ ๋ฐฉ์ ๋๋ฌธ์ JavaScript์์๋ || ์ผ๋ก ๋ณ์/์์์ ๊ธฐ๋ณธ/๋์ฒด ๊ฐ์ ํ ๋นํฉ๋๋ค.
const enteredValue = ''; // let's assume this is set based on some input provided by the user, therefore it might be an empty string
const userName = enteredValue || 'PLACEHOLDER'; // will assign 'PLACEHOLDER' if enteredValue is an empty string
quiz
์ง๋ฌธ 2:
์๋ ์ฝ๋ ๋ง์ง๋ง์ userName์ ์ ์ฅ๋์ด ์๋ ๊ฒ์ ๋ฌด์์ผ๊น์?
const enteredValue = '';
const userName = enteredValue || null;
๋ต์ 'null'
(๋น ๋ฌธ์์ด์ธ) ์ฒซ ๋ฒ์งธ ๊ฐ์ Falsy์ด๋ฏ๋ก ๋ ๋ฒ์งธ ๊ฐ(๋ ๋ฒ์งธ ํผ์ฐ์ฐ์)์ด ๋ฐํ๋ฉ๋๋ค - ๊ทธ ๋ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ Falsy๋ผ๊ณ ํ๋๋ผ๋ (์ด ์์์ฒ๋ผ).
์ง๋ฌธ 4:
์๋ ์ฝ๋ ๋ง์ง๋ง์ userName์ ์ ์ฅ๋์ด ์๋ ๊ฒ์ ๋ฌด์์ผ๊น์?
const enteredValue = 'Max';
const userName = enteredValue && 'Anna';
๋ต์ 'Anna'
์ฒซ ๋ฒ์งธ ๊ฐ์ด Truthy๋ผ๋ฉด && ์ฐ์ฐ์๋ ํญ์ ๋ ๋ฒ์งธ ๊ฐ (๋ ๋ฒ์งธ ํผ์ฐ์ฐ์)์ ๋ฐํํฉ๋๋ค.
์ง๋ฌธ 5:
์๋ ์ฝ๋ ๋ง์ง๋ง์ userName์ ์ ์ฅ๋์ด ์๋ ๊ฒ์ ๋ฌด์์ผ๊น์?
const enteredValue = '';
const userName = enteredValue && 'Anna';
๋ต์ " " (๋น๋ฌธ์์ด)
์ฒซ ๋ฒ์งธ ๊ฐ์ด Falsy๋ผ๋ฉด && ์ฐ์ฐ์๋ ํญ์ ๋ ๋ฒ์งธ ํผ์ฐ์ฐ์๊ฐ ์๋ ์ฒซ ๋ฒ์งธ ํผ์ฐ์ฐ์๋ฅผ ๋ฐํํฉ๋๋ค.
์ง๋ฌธ 6:
์๋ ์ฝ๋ ๋ง์ง๋ง์ userName์ ์ ์ฅ๋์ด ์๋ ๊ฒ์ ๋ฌด์์ผ๊น์?
const enteredValue = 'Max';
const userName = enteredValue && '';
๋ต์ " " (๋น ๋ฌธ์์ด)
์ฒซ ๋ฒ์งธ ๊ฐ์ด Truthy๋ผ๋ฉด, && ์ฐ์ฐ์๋ ํด๋น ํผ์ฐ์ฐ์๊ฐ Falsy๋ผ๊ณ ํ๋๋ผ๋ ํญ์ ๋ ๋ฒ์งธ ๊ฐ (๋ ๋ฒ์งธ ํผ์ฐ์ฐ์)์ ๋ฐํํฉ๋๋ค.
Switch-case statement
The switch statement is used to perform different actions based on different conditions.
The JavaScript Switch Statement
Use the switch statement to select one of many code blocks to be executed.
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
This is how it works:
- The switch expression is evaluated once.
- The value of the expression is compared with the values of each case.
- If there is a match, the associated block of code is executed.
- If there is no match, the default code block is executed.
Loops
Break & Continue
The break statement "jumps out" of a loop.
The continue statement "jumps over" one iteration in the loop.
The Break Statement
You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump out" of a switch() statement.
The break statement can also be used to jump out of a loop:
for (let i = 0; i < 10; i++) {
if (i === 3) { break; }
text += "The number is " + i + "<br>";
}
In the example above, the break statement ends the loop ("breaks" the loop) when the loop counter (i) is 3.
The Continue Statement
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
This example skips the value of 3:
for (let i = 0; i < 10; i++) {
if (i === 3) { continue; }
text += "The number is " + i + "<br>";
}
quiz
์ง๋ฌธ 4:
์๋์ ์ฝ๋์ ๊ฒฐ๊ณผ๋ ๋ฌด์์ผ๊น์?
let sum = 0;
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 2; j++) {
sum = sum + i + j;
break;
}
}
console.log(sum); // ???
๋ต์ 10
break๋ ๊ฐ์ฅ ๊ฐ๊น์ด์ ์๋ ๋ฐ๋ณต๋ฌธ์ ๋ฉ์ถค
์ง๋ฌธ 5:
์๋์ ์ฝ๋์ ๊ฒฐ๊ณผ๋ ๋ฌด์์ผ๊น์?
let sum = 0;
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 2; j++) {
sum = sum + i + j;
continue;
}
}
console.log(sum); // ???
์ ๋ต์ 25
continue๋ ์ฌ๊ธฐ์์ ์๋ฌด๋ฐ ์ํฅ์ด ์์ต๋๋ค. ๋ชจ๋ ๋ด๋ถ ๋ฐ๋ณต๋ฌธ ์ฝ๋๊ฐ ์คํ๋ ํ์ continue๋ฅผ ํธ์ถํ์ฌ ๋ค์ ๋ฐ๋ณต์ผ๋ก ๋์ด๊ฐ๋๋ค - ์ด์จ๋ ์ด๋ ๊ฒ ์คํ๋๊ฒ ์ง๋ง์. ์ธ๋ถ ๋ฐ๋ณต๋ฌธ์ continue์ ์ํฅ์ ๋ฐ์ง ์์ต๋๋ค("๊ฐ์ฅ ๊ฐ๊น์ด" ๋ฐ๋ณต๋ฌธ์ ์ํฅ์ ๋ฏธ์นจ).
์ง๋ฌธ 6:
์๋์ ์ฝ๋์ ๊ฒฐ๊ณผ๋ ๋ฌด์์ผ๊น์?
let sum = 0;
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 2; j++) {
if (i >= 2) {
continue;
}
sum = sum + i + j;
}
}
์ ๋ต์ 4
๋ด๋ถ ๋ฐ๋ณต๋ฌธ์ i ๊ฐ์ด 2์ ๋๋ฌํ๋ฉด ๋ฌด์ฉ์ง๋ฌผ์ด ๋ฉ๋๋ค. ๊ทธ ์์ ๋ถํฐ sum์ ๋ ์ด์ ๋ฐ๋์ง ์์ต๋๋ค. ๋ฐ๋ผ์ ๊ฒฐ๊ณผ๋ก
0 (i) + 0 (j) +
0 (i) + 1 (j) +
1 (i) + 0 (j) +
1 (i) + 1 (j) = 4์ ๊ฐ์ด ์คํ๋ฉ๋๋ค
Error Handling
์ ์ดํ ์ ์๋ ์ค๋ฅ (์: ๋คํธ์ํฌ ์ค๋ฅ) ๊ฐ ์์ต๋๋ค. ๋ฐํ์ ์คํฌ๋ฆฝํธ ์ถฉ๋์ ๋ฐฉ์งํ๊ณ ๋์ฒด๋ฅผ ์ ๊ณตํ๊ธฐ ์ํด, ์ด๋ฐ ์ค๋ฅ๋ค์ try-catch๋ฅผ ํตํด ์ฒ๋ฆฌ๋์ด์ผ ํฉ๋๋ค.
Sources
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
Operator precedence - JavaScript | MDN
Operator precedence determines how operators are parsed concerning each other. Operators with higher precedence become the operands of operators with lower precedence.
developer.mozilla.org