javascript

    [JavaScript] var๋ฅผ ์ง€์–‘ํ•ด์•ผํ•˜๋Š” ์ด์œ 

    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ณต๋ถ€๋ฅผ ํ•˜๋‹ค๋ณด๋‹ˆ var ๋Œ€์‹  let์ด๋‚˜ const๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋”๋ผ. ์šฐ์„ , var์€ ๋™์ผํ•œ ๋ณ€์ˆ˜ ์ด๋ฆ„์œผ๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ๋‹ด์•„๋„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด ๋•Œ, ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์„ ์–ธ๋œ name์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. ํŽธํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์‚ฌ์‹ค ๊ต‰์žฅํžˆ ์œ„ํ—˜ํ•œ ๊ธฐ๋Šฅ์ด๋‹ค. ๊ฐœ๋ฐœ ์ค‘ ์ฝ”๋“œ ๊ธธ์ด๊ฐ€ ๊ธธ์–ด์ง€๋‹ค๋ณด๋ฉด ๋‚ด๊ฐ€ ์„ ์–ธํ•ด์ค€ ๋ณ€์ˆ˜ ์ด๋ฆ„์ด ๊ธฐ์–ต์ด ๋‚˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, ์ด ๊ณผ์ •์—์„œ ๊ฐ’์ด ๋ฐ”๋€Œ์–ด๋ฒ„๋ฆฐ๋‹ค๋ฉด.. ๊ทธ ๋‚  ์ด์ œ ์šธ๋ฉด์„œ ๋Šฆ๊ฒŒ ์ž๋Š”๊ฑฐ์ž„ var name = "Jedi"; var name = "Yujin"; ๋ฐ˜๋ฉด, let์€ var๊ณผ ๋‹ค๋ฅด๊ฒŒ ์„ ์–ธํ–ˆ๋˜ ๋ณ€์ˆ˜๋ฅผ ์žฌ์„ ์–ธํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์•„๊นŒ ์–ธ๊ธ‰ํ•œ ์‹ค์ˆ˜๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. let name = "Jedi"; let name = "Yujin"; // Err..

    [JavaScript] async & await | ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ

    async & await ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ํŒจํ„ด์œผ๋กœ, ๊ธฐ์กด์˜ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๋ฐฉ์‹์ธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์™€ Promise์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๋Š” ํ‚ค์›Œ๋“œ๋ผ๊ณ  ํ•œ๋‹ค. (์•„์ง Promise๋„ ์ž˜ ๋ชจ๋ฅด๋Š”๋ฐ..^^) async function ํ•จ์ˆ˜๋ช…(){ await ๋น„๋™๊ธฐ์ฒ˜๋ฆฌ๋ฉ”์„œ๋“œ๋ช…(); } async async ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ค€ ํ•จ์ˆ˜์˜ return ๊ฐ’์€ ๋น„๋™๊ธฐ ๊ฐ์ฒด์ธ Promise์˜ resolve ๊ฐ’์ด ๋œ๋‹ค. ์ฆ‰, Promise์™€ ๋™์ผํ•œ ๊ฒฐ๊ณผ๊ฐ’์„ ์–ป๋Š”๋‹ค. async function helloAsync(){ return new Promise(function(resolve, reject){ var s = "Hello, Async!"; resolve(s) }); } async function logSentence(){ var s..

    [JavaScript] Promise | ์ฝœ๋ฐฑ์ง€์˜ฅ์„ ํƒˆ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•

    ๋™๊ธฐ & ๋น„๋™๊ธฐ ๊ธ€์—์„œ ๋“ฑ์žฅํ•œ ์ฝœ๋ฐฑ ์ง€์˜ฅ์„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ Promise ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Promise๋ž€? : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋น„๋™๊ธฐ๋ฅผ ๋•๋Š” ๊ฐ์ฒด์ด๋‹ค. ์ฝœ๋ฐฑ์„ ์ค„์ง€์–ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•ด์ค€๋‹ค. ์šฐ์„ , ๋น„๋™๊ธฐ์ž‘์—…์ด ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋Œ€๊ธฐ(Pending): ๋น„๋™๊ธฐ ์ƒํƒœ๊ฐ€ ์ž‘์—… ์ค‘์ด๊ฑฐ๋‚˜ ์‹œ์ž‘ํ•  ์ˆ˜ ์—†๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ ์ดํ–‰(Fulfilled): ๋น„๋™๊ธฐ ์ƒํƒœ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ง„ํ–‰๋œ ์ƒํƒœ ๊ฑฐ๋ถ€(Rejected): ๋ฒ„๊ทธ, ๋˜๋Š” ์‹คํŒจํ•œ ์ƒํƒœ๋กœ ๋น„๋™๊ธฐ ์ž‘์—…์ด ์ •์ƒ์ ์œผ๋กœ ์ง„ํ–‰๋˜์ง€ ์•Š์€ ์ƒํƒœ resolve: ๋Œ€๊ธฐ => ์ดํ–‰ reject: ๋Œ€๊ธฐ => ๊ฑฐ๋ถ€ function isPositiveP(number) { const executor = (resolve, reject) => { setTimeo..

    [JavaScript] ๋™๊ธฐ & ๋น„๋™๊ธฐ

    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์ฝ”๋“œ๊ฐ€ ์ž‘์„ฑ๋œ ์ˆœ์„œ๋Œ€๋กœ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, ์ด์ „ ์ž‘์—…์ด ์ง„ํ–‰ ์ค‘์ผ ๋•Œ๋Š” ๋‹ค์Œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ  ๊ธฐ๋‹ค๋ฆฐ๋‹ค. ์ด๋Ÿฐ Task ์ฒ˜๋ฆฌ ๋ฐฉ์‹์„ ๋™๊ธฐ ๋ฐฉ์‹์˜ ์ฒ˜๋ฆฌ๋ผ๊ณ  ํ•œ๋‹ค. function TaskA() { console.log("TASK A"); } function TaskB() { console.log("TASK B"); } function TaskC() { console.log("TASK C"); } TaskA(); TaskB(); TaskC(); ๋™๊ธฐ์  ์ฒ˜๋ฆฌ์˜ ๋‹จ์ ์€ ํ•˜๋‚˜์˜ ์ž‘์—…์ด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค๋ฉด ๋ชจ๋“  ์ž‘์—…์ด ํ•ด๋‹น ์ž‘์—…์ด ์ข…๋ฃŒ๋˜๊ธฐ ์ „๊นŒ์ง€ STOP ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒด ํ๋ฆ„์ด ์ง€์ฒด๋œ๋‹ค. ์ฆ‰, ์„ฑ๋Šฅ ์ƒ์˜ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋‹ค๋ฅธ ์–ธ์–ด(C, C++ ... )์—์„œ๋Š” ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ..

    [JavaScript] spread | ์ „๊ฐœ ์—ฐ์‚ฐ์ž

    ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž('...')๋Š” ์—ฐ๊ฒฐ, ๋ณต์‚ฌ ๋“ฑ์˜ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด๋ฅผ ํ•œ ์ค„๋กœ ํŽผ์น˜๋Š” ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐฐ์—ด์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. const coffee = ["์•„๋ฉ”๋ฆฌ์นด๋…ธ", "๋ฐ”๋‹๋ผ๋ผ๋–ผ"]; const juice = ["ํฌ๋„์ฃผ์Šค", "์˜ค๋ Œ์ง€์ฃผ์Šค", "ํ† ๋งˆํ† ์ฃผ์Šค"]; const menu = [...coffee, "์ƒˆ๋กœ์šด ์Œ๋ฃŒ", ...juice]; console.log(menu); // ["์•„๋ฉ”๋ฆฌ์นด๋…ธ", "๋ฐ”๋‹๋ผ๋ผ๋–ผ", "์ƒˆ๋กœ์šด ์Œ๋ฃŒ", "ํฌ๋„์ฃผ์Šค", "์˜ค๋ Œ์ง€์ฃผ์Šค", "ํ† ๋งˆํ† ์ฃผ์Šค"] ์ƒ๋‹นํžˆ ์ง๊ด€์ ์ธ ์˜ˆ์‹œ๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค ^-^! ๊ฐ์ฒด์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. const latte = { base: "milk", from: "Starbucks" }; const bani..

    [JavaScript] ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น ๊ทธ๋ฆฌ๊ณ  SWAP

    ๋ฐฐ์—ด์˜ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น ๋ฐฐ์—ด์—์„œ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ๋ฐฐ์—ด ์•ˆ์— ์žˆ๋Š” ๊ฐ’์„ ์ถ”์ถœํ•ด์„œ ๋ณ€์ˆ˜ / ์ƒ์ˆ˜๋กœ ๋ฐ”๋กœ ์„ ์–ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ฐฐ์—ด ์•ˆ์— ์žˆ๋Š” ์›์†Œ๋ฅผ ์ƒˆ๋กœ ์„ ์–ธํ•ด์ฃผ๊ณ  ์‹ถ์„ ๋•Œ ๊ต‰์žฅํžˆ ์œ ์šฉํ•œ ๋ฌธ๋ฒ•์ด๋‹ค. # Case 1: ๊ธฐ์กด ํ• ๋‹น ๋ฐฉ๋ฒ• let colors = ["red", "green", "blue"]; let color0 = colors[0]; let color1 = colors[1]; let color2 = colors[2]; console.log(color0, color1, color2); // red green blue # Case 2: ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น let [color1, color2, color3] = ["red", "green", "blue"]; console.log(color0, color..

    [JavaScript] Truthy & Falsy

    Truthy: ์ฐธ์ด ์•„๋‹ˆ๋ผ ์ฐธ๊ฐ™์€ ๊ฐ’ Falsy: ๊ฑฐ์ง“์ด ์•„๋‹ˆ๋ผ ๊ฑฐ์ง“๊ฐ™์€ ๊ฐ’ ์ด๊ฒŒ ๋ฌด์Šจ ์†Œ๋ฆฌ์ธ๊ฐ€ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž Truthy ex: {} ๋นˆ ๊ฐ์ฒด [] ๋นˆ ๋ฐฐ์—ด 0์„ ์ œ์™ธ ํ•œ ์ˆซ์ž๊ฐ’ ๋นˆ ๋ฌธ์ž์—ด์„ ์ œ์™ธํ•œ ๋ฌธ์ž์—ด Falsy ex: null defined 0 ๋นˆ ๋ฌธ์ž์—ด NaN ์˜ˆ์‹œ๋ฅผ ๋ณด๋‹ˆ๊น ๊ฐ์ด ์˜จ๋‹ค. ํŒŒ์ด์ฌ์—์„œ๋„ ๋นˆ ๋ฐฐ์—ด์ธ์ง€ ํ™•์ธํ•  ๋•Œ # python arr = [] if not arr: print("๋นˆ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.") else: print("๋นˆ ๋ฐฐ์—ด์ด ์•„๋‹™๋‹ˆ๋‹ค.") ์œ„ ์˜ˆ์‹œ์ฒ˜๋Ÿผ arr๋ฅผ true/false๋กœ ๋ฐ˜ํ™˜ํ•˜์—ฌ ํŒ๋‹จํ•˜๋Š”๋ฐ, ์ด๋Ÿฐ ๊ฑธ truthy/falsy๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ์œ„์˜ Python ์˜ˆ์‹œ๋ฅผ JavaScript๋กœ ๋ฐ”๊ฟ”๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. # JavaScript const arr = [] if (!a..

    [JavaScript] ๋ฐฐ์—ด ๋‚ด์žฅ ํ•จ์ˆ˜

    forEach ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. forEach ๊ตฌ๋ฌธ์˜ ์ธ์ž๋กœ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•  ๋•Œ ํ•ด๋‹น ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. ์ด๋•Œ, ๋ฐฐ์—ด ์š”์†Œ์˜ ์ธ๋ฑ์Šค์™€ ๊ฐ’์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค. const arr = [1, 2, 3, 4, 5]; arr.forEach((elm) => console.log(elm * 2)); arr.forEach(function(item, index){ console.log(item, index); }); // item: ๋ฐฐ์—ด ๊ฐ๊ฐ์˜ ์•„์ดํ…œ & index: ๋ฐฐ์—ด ์•„์ดํ…œ์˜ index map ์›๋ณธ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์—ฐ์‚ฐ์„ ํ•œ ํ›„, return ๊ฐ’์œผ๋กœ ํ•ด๋‹น ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค. const arr = [1, 2, 3, 4, ..