๋ฐ˜์‘ํ˜•

TL;DR


๏ฟญ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ”„๋กœ๊ทธ๋žจ์€ ์ผ๋ จ์˜ ๋ฌธ(Statement)์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค

๏ฟญ ๊ฐ ๋ฌธ์€ ๋ณ€์ˆ˜ ์ƒ์„ฑ, if, else ์กฐ๊ฑด ์‹คํ–‰, ๋ฃจํ”„ ์‹œ์ž‘๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ช…๋ น์ด๋‹ค

๏ฟญ ํ‘œํ˜„์‹์€ ๊ฐ’์„ ์ƒ์„ฑํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋‹ค → ํ‘œํ˜„์‹์€ ํ•ญ์ƒ ๊ฐ’์„ ์ƒ์„ฑํ•œ๋‹ค

๏ฟญ ๋ชจ๋“  ํ•จ์ˆ˜์˜ ์ธ์ž๋Š” ํ‘œํ˜„์‹์ด๋‹ค. ๋ฌธ์€ ๊ฐ’์„ ์ƒ์„ฑํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ํ•จ์ˆ˜ ์ธ์ž์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค

๏ฟญ ๋ฌธ์€ ํ”„๋กœ๊ทธ๋žจ์„ ํ•˜๋‚˜๋กœ ๋ฌถ๋Š” ๋‹จ๋‹จํ•œ ๊ตฌ์กฐ์ด๋ฉฐ, ๋ฌธ์˜ ์„ธ๋ถ€์‚ฌํ•ญ์€ ํ‘œํ˜„์‹์œผ๋กœ ์ฑ„์›Œ์ง„๋‹ค

๏ฟญ ํ‘œํ˜„์‹์€ ๋‹จ๋…์œผ๋กœ ์กด์žฌํ•  ์ˆ˜ ์—†๊ณ , ํ•ญ์ƒ ๋ฌธ์˜ ์ผ๋ถ€๋‹ค

๏ฟญ ๋ฌธ์€ ํ‘œํ˜„์‹ ์Šฌ๋กฏ์„ ์ œ์™ธํ•˜๊ณค ๋ณธ์งˆ์ ์œผ๋กœ ๋น„์–ด์žˆ๋‹ค

๏ฟญ ๋ฌธ์ด ๋น„์–ด ์žˆ์„ ๋•Œ๋„ ํ‘œํ˜„์‹์€ ํ•ญ์ƒ ๋ฌธ์˜ ์ผ๋ถ€๋‹ค

๏ฟญ JSX์˜ {} ์ค‘๊ด„ํ˜ธ๋Š” ํ‘œํ˜„์‹ ์Šฌ๋กฏ์„ ๋งŒ๋“ ๋‹ค

๏ฟญ ํ‘œํ˜„์‹ ์Šฌ๋กฏ์—์„  ๊ฐ’์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š” ๋ฌธ์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค

 

ํ‘œํ˜„์‹ | Expressions


๏ฟญ ํ‘œํ˜„์‹์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ’์„ ์ƒ์„ฑํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋‹ค

๏ฟญ ์ฆ‰, ํ‘œํ˜„์‹์€ ํ•ญ์ƒ ๊ฐ’์„ ์ƒ์„ฑํ•œ๋‹ค

๏ฟญ ์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋ชจ๋‘ ํ‘œํ˜„์‹์ด๋‹ค

// ์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋ชจ๋‘ ํ‘œํ˜„์‹์ด๋‹ค
1                    // 1 ์ƒ์„ฑ
("hello")            // 'hello' ์ƒ์„ฑ
5 * 10               // 50 ์ƒ์„ฑ
num > 100            // true | false ์ƒ์„ฑ
isHappy ? "๐Ÿ‡น๐Ÿ‡ญ" : "๐Ÿ‡ฐ๐Ÿ‡ท" // ์ด๋ชจ์ง€ ์ƒ์„ฑ
[1, 2, 3].pop()      // 3 ์ƒ์„ฑ

 

๏ฟญ ํ‘œํ˜„์‹์€ ํ‘œํ˜„์‹์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค.

๏ฟญ (5 + 1) * 2 ํ‘œํ˜„์‹ ์•ˆ์—” ๋ช‡ ๊ฐœ์˜ ํ‘œํ˜„์‹์ด ์žˆ์„๊นŒ?

์ธํ„ฐ๋ ‰์…˜ ์ถœ์ฒ˜ Josh Comeau

  1. ์ „์ฒด ํ‘œํ˜„์‹ : (5 + 1) * 212๋กœ ํ‰๊ฐ€
  2. ๊ด„ํ˜ธ์•ˆ ๋ง์…ˆ : (5 + 1)6์œผ๋กœ ํ‰๊ฐ€
  3. ๊ด„ํ˜ธ์•ˆ ์ฒซ๋ฒˆ์งธ ์ˆซ์ž : 55๋กœ ํ‰๊ฐ€
  4. ๊ด„ํ˜ธ์•ˆ ๋‘๋ฒˆ์งธ ์ˆซ์ž : 11๋กœ ํ‰๊ฐ€
  5. ๊ณฑํ•˜๋Š” ์ˆ˜ : 22๋กœ ํ‰๊ฐ€

 

๋ฌธ | Statements


๏ฟญ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ”„๋กœ๊ทธ๋žจ์€ ์ผ๋ จ์˜ ๋ฌธ(Statement)์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค

๏ฟญ ๊ฐ ๋ฌธ์€ ์ปดํ“จํ„ฐ์—๊ฒŒ ํŠน์ • ์ž‘์—…์„ ์ง€์‹œํ•œ๋‹ค

๏ฟญ ์•„๋ž˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ฌธ์˜ ์˜ˆ์‹œ

let num = 5;
if (num > 10) { /* More statements here */ }
throw new Error("Something exploded!");

 

๏ฟญ ๋ฌธ์€ ํ”„๋กœ๊ทธ๋žจ์„ ํ•˜๋‚˜๋กœ ๋ฌถ๋Š” ๋‹จ๋‹จํ•œ ๊ตฌ์กฐ์ด๋ฉฐ, ๋ฌธ์˜ ์„ธ๋ถ€์‚ฌํ•ญ์€ ํ‘œํ˜„์‹์œผ๋กœ ์ฑ„์›Œ์ง„๋‹ค

๏ฟญ ๋ฌธ์—๋Š” ํ‘œํ˜„์‹์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ์Šฌ๋กฏ์ด ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค

๏ฟญ ํ‘œํ˜„์‹ ์Šฌ๋กฏ์—” ๋ชจ๋“  ์ข…๋ฅ˜์˜ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

๏ฟญ ๋ณ€์ˆ˜ ์„ ์–ธ์—๋„ ํ‘œํ˜„์‹ ์Šฌ๋กฏ์ด ์žˆ๋‹ค(ํ• ๋‹น๋ฌธ)

let hi = /* ํ‘œํ˜„์‹ ์Šฌ๋กฏ */;

let hi = 1;
let hi = 'hello';
let hi = 5 * 10;
let hi = num > 100;
let hi = isHappy ? "๐Ÿ‡น๐Ÿ‡ญ" : "๐Ÿ‡ฐ๐Ÿ‡ท";
let hi = [1, 2, 3].pop();

 

๏ฟญ ํ• ๋‹น๋ฌธ์€ ๊ฐ’์œผ๋กœ ํ‰๊ฐ€๋˜๋ฏ€๋กœ ํ‘œํ˜„์‹์ธ ๋ฌธ์ด๋‹ค

๏ฟญ ์„ ์–ธ๋ฌธ์€ ๊ฐ’์œผ๋กœ ํ‰๊ฐ€๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ํ‘œํ˜„์‹์ด ์•„๋‹Œ ๋ฌธ์ด๋‹ค

๏ฟญ ํฌ๋กฌ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ์—์„œ ํ‘œํ˜„์‹์ด ์•„๋‹Œ ๋ฌธ์€ ํ•ญ์ƒ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ํ‘œํ˜„์‹์ธ ๋ฌธ์€ ํ•ญ์ƒ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค

let hi; // ์„ ์–ธ๋ฌธ | ํ‘œํ˜„์‹์ด ์•„๋‹Œ ๋ฌธ
// undefined | ํ‘œํ˜„์‹์ด ์•„๋‹Œ ๋ฌธ์ด๋ฏ€๋กœ undefined ๋ฐ˜ํ™˜(์ถœ๋ ฅ)

let hi = 1; // ํ• ๋‹น๋ฌธ | ํ‘œํ˜„์‹์ธ ๋ฌธ
// 1 | ํ‘œํ˜„์‹์ธ ๋ฌธ์ด๋ฏ€๋กœ 1์ด๋ผ๋Š” ๊ฐ’์„ ํ‰๊ฐ€ํ•˜์—ฌ 1 ๋ฐ˜ํ™˜(์ถœ๋ ฅ)

 

๏ฟญ ๋ฌธ์— ํ‘œํ˜„์‹ ์Šฌ๋กฏ์ด ์žˆ๊ณ , ์Šฌ๋กฏ์— ํ‘œํ˜„์‹์„ ๋„ฃ์œผ๋ฉด ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค. ๋ฌธ๋ฒ• ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค

๏ฟญ ํ•˜์ง€๋งŒ ์•„๋ž˜ ๊ฐ™์€ ์ฝ”๋“œ์—์„  ๋ฌธ๋ฒ• ์—๋Ÿฌ๋Š” ์—†์ง€๋งŒ ์‹คํ–‰ํ•˜๋ฉด ๋ฌดํ•œ ๋ฃจํ”„๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค

while ('hello') {
  // while ์กฐ๊ฑด์‹ ๊ด„ํ˜ธ๊ฐ€ ํ‘œํ˜„์‹ ์Šฌ๋กฏ์ด๋‹ค
  // 'hello'๋Š” ๋ฐ”๋€Œ์ง€ ์•Š๋Š” ๊ฐ’์ด๋ฏ€๋กœ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ถฉ๋Œํ•  ๋•Œ๊นŒ์ง€ ๊ณ„์† ์‹คํ–‰๋œ๋‹ค
  // ๋ฌธ์ œ๊ฐ€ ์—†๋Š” ๋ฌธ๋ฒ•์ด์ง€๋งŒ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”(?) ์ƒํ™ฉ
}

 

๋ฌธ / ํ‘œํ˜„์‹ ํ™•์ธ ๋ฐฉ๋ฒ•


console.log(/* Some chunk of JS here */);

 

๏ฟญ ํŠน์ • ์ฝ”๋“œ ์กฐ๊ฐ์ด ๋ฌธ / ํ‘œํ˜„์‹์ธ์ง€ ํ™•์ธํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์ฝ˜์†” ๋กœ๊ทธ๋ฅผ ํ™œ์šฉํ•˜์ž

๏ฟญ ๋ฌธ์ œ์—†์ด ์‹คํ–‰๋˜๋ฉด ์ž…๋ ฅํ•œ ์ฝ”๋“œ ์กฐ๊ฐ์€ ํ‘œํ˜„์‹์ด๋‹ค

๏ฟญ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋ฌธ ํ˜น์€ ์ž˜๋ชป๋œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋‹ค

๏ฟญ ๋ธŒ๋ผ์šฐ์ € ์ฝ˜์†”์—์„  ํ‘œํ˜„์‹์„ ์–ด๋–ป๊ฒŒ ํ•ด์„(ํ‰๊ฐ€)ํ•˜๋Š”์ง€ ์ถœ๋ ฅ๊ฐ’์„ ํ†ตํ•ด ๋ฐ”๋กœ ์•Œ ์ˆ˜ ์žˆ๋‹ค

๏ฟญ ๋ชจ๋“  ํ•จ์ˆ˜์˜ ์ธ์ž๋Š” ํ‘œํ˜„์‹์ด๊ธฐ ๋•Œ๋ฌธ์— ์œ„ ๊ฐ™์€ ํŠธ๋ฆญ์ด ๊ฐ€๋Šฅํ•œ ๊ฒƒ

๏ฟญ ํ‘œํ˜„์‹์€ ๊ฐ’์„ ์ƒ์„ฑํ•˜๊ณ  ํ•จ์ˆ˜๋กœ ์ „๋‹ฌ๋œ๋‹ค

๏ฟญ ๋ฌธ์€ ๊ฐ’์„ ์ƒ์„ฑํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ํ•จ์ˆ˜ ์ธ์ž์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค

 

ํ‘œํ˜„์‹์ธ ๋ฌธ (Expressions as statements)


๏ฟญ 1 + 2 + 3 ํ‘œํ˜„์‹์„ ์•„๋ฌด ๋‚ด์šฉ๋„ ์—†๋Š” test.js ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž

๏ฟญ test.js ํŒŒ์ผ์—” ๋ช‡ ๊ฐœ์˜ ๋ฌธ์ด ์žˆ๋Š”๊ฑธ๊นŒ? 0๊ฐœ?

๏ฟญ ํ‘œํ˜„์‹์€ ๋‹จ๋…์œผ๋กœ ์กด์žฌํ•  ์ˆ˜ ์—†๋‹ค. ํ‘œํ˜„์‹์€ ํ•ญ์ƒ ๋ฌธ์˜ ์ผ๋ถ€๋‹ค

๏ฟญ ๋”ฐ๋ผ์„œ test.js ํŒŒ์ผ์—” ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฌธ์ด ์žˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค

/* ํ‘œํ˜„์‹ ์Šฌ๋กฏ */

 

๏ฟญ ๋ฌธ์€ ํ‘œํ˜„์‹ ์Šฌ๋กฏ์„ ์ œ์™ธํ•˜๊ณค ๋ณธ์งˆ์ ์œผ๋กœ ๋น„์–ด์žˆ๋‹ค

๏ฟญ ๋ฌธ์˜ ํ‘œํ˜„์‹ ์Šฌ๋กฏ์— 1 + 2 + 3 ํ‘œํ˜„์‹์„ ์ฑ„์šฐ ๋ฌธ์ด ์™„์„ฑ๋œ๋‹ค

๏ฟญ ์•„๋ž˜๋Š” ๋ชจ๋‘ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ด๋‹ค

// ๋ฌธ 1 (ํ• ๋‹น๋ฌธ)
let hi = /* ํ‘œํ˜„์‹ ์Šฌ๋กฏ */;

// ๋ฌธ 2 (์กฐ๊ฑด๋ฌธ)
if (/* ํ‘œํ˜„์‹ ์Šฌ๋กฏ */) { }

// ๋ฌธ 3 (๋ฆฌํ„ด๋ฌธ)
return /* ํ‘œํ˜„์‹ ์Šฌ๋กฏ */;

// ๋ฌธ 4
/* ํ‘œํ˜„์‹ ์Šฌ๋กฏ */

 

๏ฟญ ํ‘œํ˜„์‹๊ณผ ๋ฌธ์€ ๋ณ„๊ฐœ๋‹ค. ํ‘œํ˜„์‹์ด ๋ฌธ์ด๋ผ๊ณ  ์„ค๋ช…ํ•˜๋Š”๊ฑด ์ž˜๋ชป๋œ ๊ฒƒ

๏ฟญ ์œ„์ฒ˜๋Ÿผ ์ถ”๊ฐ€์ ์ธ ๋ฌธ์ž๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š์•„๋„ ๋ฌธ์€ ํ‘œํ˜„์‹์„ ๊ฐ์Œ€ ์ˆ˜ ์žˆ๋‹ค

๏ฟญ ํˆฌ๋ช…ํ•œ ๋น„๋‹ ๋žฉ(๋ฌธ)์œผ๋กœ ์ƒŒ๋“œ์œ„์น˜(ํ‘œํ˜„์‹)๋ฅผ ํฌ์žฅํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ•˜๋‹ค

๏ฟญ ๋ฌธ์˜ ๋์—” ; ์„ธ๋ฏธ์ฝœ๋ก ์„ ๋ถ™์ธ๋‹ค(๋ฌธ์˜ ์ข…๋ฃŒ๋ฅผ ๋‚˜ํƒ€๋ƒ„)

๏ฟญ 0๊ฐœ ์ด์ƒ์˜ ๋ฌธ์„ {} ์ค‘๊ด„ํ˜ธ๋กœ ๋ฌถ์€ ์ฝ”๋“œ ๋ธ”๋ก ๋’ค์—” ๋ถ™์ด์ง€ ์•Š๋Š”๋‹ค(if, while, for, ํ•จ์ˆ˜ ์„ ์–ธ ๋“ฑ)

๏ฟญ ์„ธ๋ฏธ์ฝœ๋ก ์€ ์˜ต์…˜์ด๋‹ค

 

JSX์—์„œ ์ค‘๊ด„ํ˜ธ โšก๏ธ


๏ฟญ (React) JSX ์•ˆ์—์„œ {} ์ค‘๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค

function CountdownClock({ secondsRemaining }) {
  return (
    <div>
      Time left:
      {Math.round(secondsRemaining / 60)} minutes!
    </div>
  );
}

 

๏ฟญ ํ•˜์ง€๋งŒ JSX ์ค‘๊ด„ํ˜ธ {} ์•ˆ์—์„œ if, else ๊ฐ™์€ ๋ฌธ(Statement)์„ ์‚ฌ์šฉํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค

function CountdownClock({ secondsRemaining }) {
  return (
    <div>
      {
        // ๐Ÿšซ Error!
        if (secondsRemaining > 0) { `${secondsRemaining} seconds left` }
        else { "Time expired!" }
      }
    </div>
  );
}

 

๏ฟญ JSX์—์„œ {} ์ค‘๊ด„ํ˜ธ๋Š” ํ‘œํ˜„์‹ ์Šฌ๋กฏ์„ ๋งŒ๋“ ๋‹ค

๏ฟญ ํ‘œํ˜„์‹ ์Šฌ๋กฏ์—์„œ ๊ฐ’์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š” ๋ฌธ์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค

๏ฟญ ์ฆ‰, JSX {} ์ค‘๊ด„ํ˜ธ ์•ˆ์—์„  ๊ฐ’์„ ์ƒ์„ฑํ•˜๋Š” ํ‘œํ˜„์‹๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

๏ฟญ ๋”ฐ๋ผ์„œ if, else ๋ฌธ ๋Œ€์‹  ์‚ผํ•ญ ์—ฐ์‚ฐ์ž(Ternary operator)๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค

function CountdownClock({ secondsRemaining }) {
  return (
    <div>
      {secondsRemaining > 0 // โœ… OK
        ? `${secondsRemaining} seconds left`
        : "Time expired!"}
    </div>
  );
}

 

๏ฟญ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋Š” ์กฐ๊ฑด์„ ํ‰๊ฐ€ํ•˜๊ณ  ์กฐ๊ฑด์˜ ์ฐธ/๊ฑฐ์ง“์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ‘œํ˜„์‹์ด๋‹ค

 

๋ ˆํผ๋Ÿฐ์Šค


 

Statements Vs. Expressions

One of the most foundational things to understand about JavaScript is that programs are made up of statements, and statements have slots for expressions. In this blog post, we'll dig into how these two structures work, and see how building an intuition abo

www.joshwcomeau.com

 


๊ธ€ ์ˆ˜์ •์‚ฌํ•ญ์€ ๋…ธ์…˜ ํŽ˜์ด์ง€์— ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”
๋ฐ˜์‘ํ˜•