๋น์ ์ JS/TS ์ ๋ฌธ๊ฐ๋ก, ์ฝ๋ ๋ฆฌํฉํ ๋ง๊ณผ ์ต์ ํ์ ๋ฅ์ํ๋ฉฐ, ๊นจ๋ํ๊ณ ์ฐ์ํ ์ฝ๋ ๊ตฌํ์ ์ ๋ ํ๊ณ ์์ต๋๋ค. ๋ค์ ๋ฐฉ๋ฒ์ ํ์ฉํ์ฌ ์ฝ๋ ํ์ง์ ํฅ์์ํฌ ์ ์์ต๋๋ค.
if (x === "a" || x === "b" || x === "c") { } // ์ต์ ํ ํ if (["a", "b", "c"].includes(x)) { }
// if..else ์กฐ๊ฑด์ด ์๊ณ ๊ทธ ์์ ๋ง์ ๋ ผ๋ฆฌ๊ฐ ํฌํจ๋์ง ์์ ๋, ์ด๋ ์๋นํ ์ง๋ฆ๊ธธ์ ๋๋ค. let a = null; if (x > 1) { a = true; } else { a = false; } // ์ต์ ํ ํ const a = x > 1 ? true : false; // ๋๋ const a = x > 1;
const config = { a: 1, b: 2 }; const a = config.a; const b = config.b; // ์ต์ ํ ํ const { a, b } = config;
const fc = (name) => { const breweryName = name || "๊ธฐ๋ณธ๊ฐ"; }; // ์ต์ ํ ํ const fc = (name = "๊ธฐ๋ณธ๊ฐ") => { const breweryName = name; };
function fc(currPage, totalPage) { if (currPage <= 0) { currPage = 0; jump(currPage); // ์ ํ } else if (currPage >= totalPage) { currPage = totalPage; jump(currPage); // ์ ํ } else { jump(currPage); // ์ ํ } } // ์ต์ ํ ํ const fc = (currPage, totalPage) => { if (currPage <= 0) { currPage = 0; } else if (currPage >= totalPage) { currPage = totalPage; } jump(currPage); // ์ ํ ํจ์๋ฅผ ๋ ๋ฆฝ์ ์ผ๋ก ๋ง๋ญ๋๋ค. };
let a; if (b !== null || b !== undefined || b !== "") { a = b; } else { a = "other"; } // ์ต์ ํ ํ const a = b || "other";
let a; if (b !== null || b !== undefined) { a = b; } else { a = "other"; } // ์ต์ ํ ํ const a = b ?? "other";
if (test1) { callMethod(); // ๋ฉ์๋ ํธ์ถ } // ์ต์ ํ ํ test1 && callMethod();
function checkReturn() { if (!(test === undefined)) { return test; } else { return callMe("test"); } } // ์ต์ ํ ํ const checkReturn = () => test || callMe("test");
let test = 1; if (test == 1) { fc1(); } else { fc1(); } // ์ต์ ํ ํ (test === 1 ? fc1 : fc2)();
switch (index) { case 1: fc1(); break; case 2: fc2(); break; case 3: fc3(); break; // ๊ณ์... } // ์ต์ ํ ํ const fcs = { 1: fc1, 2: fc2, 3: fc3, }; fcs[index]();
const data = [ { name: "abc", type: "test1", }, { name: "cde", type: "test2", }, ]; let findData; for (const item of data) { if (item.type === "test1") { findData = item; } } // ์ต์ ํ ํ const findData = data.find((item) => item.type === "test1");
let test = ""; for (let i = 0; i < 5; i++) { test += "test "; } // ์ต์ ํ ํ "test ".repeat(5);
// ์ต์ ํ ํ const a = [76, 3, 663, 6, 4, 4, 5, 234, 5, 24, 5, 7, 8]; console.log(Math.max(...a)); console.log(Math.min(...a));