JavaScript - The Complete Guide 2022

Section 4: Control Structures - Conditional Code & Loops

olivia_yj 2022. 10. 13. 23:40

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