- Published on
π JavaScript μ μ©ν μ νΈλ¦¬ν° ν¨μ λ° ES2020+ μ΅μ λ¬Έλ² μ 리(2025 κΈ°μ€)
1. μ μ©ν μ νΈλ¦¬ν° ν¨μλ€
μκ³ λ¦¬μ¦ λ¬Έμ ν΄κ²°μ μμ£Ό μ¬μ©λλ μ νΈλ¦¬ν° ν¨μλ€μ λλ€. λ²μ κ³Ό κ΄κ³μμ΄ μ€λ¬΄μμ μ μ©ν κΈ°λ₯λ€μ μ 리νμ΅λλ€.
1.1 Math κ΄λ ¨ ν¨μλ€
Math.trunc()- μ€λͺ : μμμ μ λ²λ¦¬κ³ μ μ λΆλΆλ§ λ°νν©λλ€ (μμμ λ²λ¦Ό).
- μκ³ λ¦¬μ¦ νμ©: λλμ μ λͺ«μ ꡬνκ±°λ, μ μ λ³νμ΄ νμν λ μ¬μ©ν©λλ€.
// β κΈ°μ‘΄ λ°©μ const quotient = Math.floor(a / b) // μμμμ λ€λ₯΄κ² λμ const quotient2 = parseInt(a / b) // λλ¦¬κ³ λΆμ ν // β Math.trunc() μ¬μ© const quotient = Math.trunc(a / b) // μμμ λ²λ¦Ό (λͺ«) Math.trunc(3.7) // 3 Math.trunc(-3.7) // -3 Math.trunc(3.1) // 3 // μ€μ μμ: λλμ λͺ« ꡬνκΈ° function divide(a, b) { return Math.trunc(a / b) } // μ€μ μμ: μ μ λ³ν function toInt(n) { return Math.trunc(n) } // μ€μ μμ: μλ¦Ώμ λΆλ¦¬ function getDigits(n) { return Math.trunc(Math.abs(n)).toString().length }- μ£Όμμ :
Math.floor()μ λ¬λ¦¬ μμμμλ μμμ λ§ λ²λ¦½λλ€ (Math.floor(-3.7)= -4,Math.trunc(-3.7)= -3).parseInt()λ³΄λ€ λΉ λ₯΄κ³ μ νν©λλ€.
- κ°μ μ : λλμ
μ λͺ«μ ꡬν λ
Math.floor()λ³΄λ€ μ§κ΄μ μ΄κ³ μ νν©λλ€.
Math.max()/Math.min()- μ€λͺ : μ¬λ¬ κ° μ€ μ΅λκ°/μ΅μκ°μ λ°νν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: λ°°μ΄μ μ΅λ/μ΅μκ° μ°ΎκΈ°, λ κ° λΉκ΅, κ²½κ³κ° μ²΄ν¬ λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β κΈ°μ‘΄ λ°©μ function findMax(arr) { let max = arr[0] for (let i = 1; i < arr.length; i++) { if (arr[i] > max) max = arr[i] } return max } // β Math.max() μ¬μ© const max = Math.max(1, 5, 3, 9, 2) // 9 const maxFromArray = Math.max(...arr) // λ°°μ΄μ μ΅λκ° const min = Math.min(1, 5, 3, 9, 2) // 1 // μ€μ μμ: λ°°μ΄ μ΅λ/μ΅μκ° function getMaxMin(arr) { return { max: Math.max(...arr), min: Math.min(...arr) } } // μ€μ μμ: κ²½κ³κ° μ ν function clamp(value, min, max) { return Math.min(Math.max(value, min), max) } // μ€μ μμ: λ κ° μ€ ν°/μμ κ° μ ν function maxPath(a, b) { return Math.max(a, b) } // μ€μ μμ: μ¬λ¬ κ²½λ‘ μ€ μ΅λ¨ 거리 function shortestPath(paths) { return Math.min(...paths) }- μ£Όμμ :
- μΈμκ° μμΌλ©΄
Math.max()λ-Infinity,Math.min()μInfinityλ₯Ό λ°νν©λλ€. - λ°°μ΄μ μ§μ λ°μ§ μμΌλ―λ‘ μ€νλ λ μ°μ°μ(
...)λ₯Ό μ¬μ©ν΄μΌ ν©λλ€.
- μΈμκ° μμΌλ©΄
- κ°μ μ : μ΅λ/μ΅μκ° κ³μ°μ΄ κ°κ²°νκ³ κ°λ μ±μ΄ μ’μ΅λλ€.
Math.abs()- μ€λͺ : μ«μμ μ λκ°μ λ°νν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: 거리 κ³μ°, μ°¨μ΄κ° κ³μ°, μμ μ²λ¦¬ λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β κΈ°μ‘΄ λ°©μ function abs(n) { return n < 0 ? -n : n } // β Math.abs() μ¬μ© Math.abs(-5) // 5 Math.abs(5) // 5 Math.abs(0) // 0 // μ€μ μμ: λ μ μ¬μ΄ 거리 (μ λκ°) function manhattanDistance(x1, y1, x2, y2) { return Math.abs(x2 - x1) + Math.abs(y2 - y1) } // μ€μ μμ: μ°¨μ΄κ° κ³μ° function difference(a, b) { return Math.abs(a - b) } // μ€μ μμ: μμ λ°©μ§ function ensurePositive(n) { return Math.abs(n) } // μ€μ μμ: λ°°μ΄ μμ μ°¨μ΄μ μ΅λκ° function maxDifference(arr) { return Math.max(...arr) - Math.min(...arr) }- μ£Όμμ :
Math.abs(-0)μ0μ λ°νν©λλ€ (λΆνΈ μλ 0 μ²λ¦¬).NaNμ μ λ ₯νλ©΄NaNμ λ°νν©λλ€.
- κ°μ μ : μ λκ° κ³μ°μ΄ κ°λ¨νκ³ λͺ νν©λλ€.
Math.floor(),Math.ceil(),Math.round()- μ€λͺ : λ°μ¬λ¦Ό κ΄λ ¨ ν¨μλ€μ λλ€.
- μκ³ λ¦¬μ¦ νμ©: μ¬λ¦Ό/λ΄λ¦Ό/λ°μ¬λ¦Όμ΄ νμν κ³μ°μ μ¬μ©λ©λλ€.
// β λ°μ¬λ¦Ό ν¨μλ€ Math.floor(3.7) // 3 (λ΄λ¦Ό) Math.ceil(3.2) // 4 (μ¬λ¦Ό) Math.round(3.5) // 4 (λ°μ¬λ¦Ό) Math.floor(-3.7) // -4 (μμ λ΄λ¦Ό) Math.ceil(-3.2) // -3 (μμ μ¬λ¦Ό) // μ€μ μμ: μ¬λ¦Ό λλμ function ceilDivide(a, b) { return Math.ceil(a / b) } // μ€μ μμ: νμ΄μ§ κ³μ° function getTotalPages(items, itemsPerPage) { return Math.ceil(items / itemsPerPage) } // μ€μ μμ: λ°μ¬λ¦Ό function roundToDecimal(n, decimals) { const factor = 10 ** decimals return Math.round(n * factor) / factor } // μ€μ μμ: λ΄λ¦Ό λλμ function floorDivide(a, b) { return Math.floor(a / b) }- μ£Όμμ :
Math.floor()λ νμ μλλ‘,Math.ceil()μ νμ μλ‘ λ°μ¬λ¦Όν©λλ€.Math.round()λ 0.5μμ μλ‘ λ°μ¬λ¦Όν©λλ€ (μμ κΈ°μ€).- μμμμ
Math.floor()μMath.trunc()μ λμμ΄ λ€λ¦ λλ€.
- κ°μ μ : λ€μν λ°μ¬λ¦Ό μꡬμ¬νμ μ²λ¦¬ν μ μμ΅λλ€.
Math.hypot(...values)- μ€λͺ : νΌνκ³ λΌμ€ μ 리()λ₯Ό μ½κ² κ³μ°ν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: λ μ μ¬μ΄μ 거리 κ³μ°μ΄λ 벑ν°μ ν¬κΈ° κ³μ° μ μ¬μ©ν©λλ€.
// β κΈ°μ‘΄ λ°©μ function distance(x1, y1, x2, y2) { return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) } // β Math.hypot() μ¬μ© const dist = Math.hypot(3, 4) // 5 (3-4-5 μ§κ°μΌκ°ν) const dist2 = Math.hypot(x2 - x1, y2 - y1) // λ μ μ¬μ΄ 거리 // μ€μ μμ: μ ν΄λ¦¬λ 거리 function euclideanDistance(p1, p2) { return Math.hypot(p2.x - p1.x, p2.y - p1.y) } // μ€μ μμ: 3D 거리 function distance3D(p1, p2) { return Math.hypot(p2.x - p1.x, p2.y - p1.y, p2.z - p1.z) } // μ€μ μμ: λ²‘ν° ν¬κΈ° function vectorMagnitude(v) { return Math.hypot(...v) }- μ£Όμμ :
- μ¬λ¬ μΈμλ₯Ό λ°μ μ μμ΄μ 2D, 3D, Nμ°¨μ 거리 κ³μ°μ΄ λͺ¨λ κ°λ₯ν©λλ€.
- μ€λ²νλ‘μ°λ₯Ό λ°©μ§νκΈ° μν΄ λ΄λΆμ μΌλ‘ μ΅μ νλμ΄ μμ΅λλ€.
- κ°μ μ : 거리 κ³μ°μ΄ κ°κ²°νκ³ μ€λ²νλ‘μ° μνμ΄ μ μ΅λλ€.
1.2 Number κ΄λ ¨ ν¨μλ€
Number.isInteger()- μ€λͺ : κ°μ΄ μ μμΈμ§ νμΈν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: μ λ ₯ κ²μ¦μ΄λ νμ μ²΄ν¬ μ μ¬μ©ν©λλ€.
// β κΈ°μ‘΄ λ°©μ function isInt(n) { return n % 1 === 0 } // β Number.isInteger() μ¬μ© Number.isInteger(3) // true Number.isInteger(3.0) // true Number.isInteger(3.5) // false Number.isInteger('3') // false (λ¬Έμμ΄μ false) // μ€μ μμ: μ λ ₯ κ²μ¦ function validateInput(n) { if (!Number.isInteger(n) || n < 0) { throw new Error('μμ μ μλ§ μ λ ₯ κ°λ₯ν©λλ€') } } // μ€μ μμ: μ μ νν°λ§ function filterIntegers(arr) { return arr.filter(Number.isInteger) }- μ£Όμμ :
- λ¬Έμμ΄μ΄λ λ€λ₯Έ νμ
μ
falseλ₯Ό λ°νν©λλ€. NaNκ³ΌInfinityλfalseλ₯Ό λ°νν©λλ€.
- λ¬Έμμ΄μ΄λ λ€λ₯Έ νμ
μ
- κ°μ μ : νμ μμ ν μ μ 체ν¬κ° κ°λ₯ν©λλ€.
parseInt(),parseFloat()- μ€λͺ : λ¬Έμμ΄μ μ«μλ‘ λ³νν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: μ λ ₯ νμ±, μ§λ² λ³ν λ±μ μ¬μ©λ©λλ€.
// β λ¬Έμμ΄ μ«μ λ³ν parseInt('123') // 123 parseInt('123.45') // 123 (μ μλ§) parseFloat('123.45') // 123.45 parseInt('1010', 2) // 10 (2μ§μ) parseInt('FF', 16) // 255 (16μ§μ) // μ€μ μμ: μ λ ₯ νμ± function parseInput(input) { return parseInt(input.trim(), 10) } // μ€μ μμ: μ§λ² λ³ν function binaryToDecimal(binary) { return parseInt(binary, 2) } // μ€μ μμ: μμ ν μ«μ λ³ν function safeParseInt(str, defaultValue = 0) { const parsed = parseInt(str, 10) return isNaN(parsed) ? defaultValue : parsed } // μ€μ μμ: μ¬λ¬ μ§λ² μ§μ function parseNumber(str, radix = 10) { return parseInt(str, radix) }- μ£Όμμ :
parseInt()λ λ λ²μ§Έ μΈμ(radix)λ₯Ό λͺ μνλ κ²μ΄ μ’μ΅λλ€ (κΈ°λ³Έκ°μ΄ 10μ΄ μλλ―λ‘).parseInt('')λNaNμ λ°νν©λλ€.Number()μ λ¬λ¦¬ μλΆλΆλ§ νμ±ν©λλ€ (parseInt('123abc')= 123).
- κ°μ μ : λ¬Έμμ΄ μ λ ₯μ μ«μλ‘ λ³νν λ μ μ©ν©λλ€.
1.3 Array κ΄λ ¨ ν¨μλ€
Array.from({ length: N }, mapFn)- μ€λͺ : νΉμ κΈΈμ΄μ λ°°μ΄μ λ§λ€κ³ λμμ μ΄κΈ°νν λ μ¬μ©ν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: 2μ°¨μ λ°°μ΄ μ΄κΈ°νλ νΉμ ν¨ν΄μ λ°°μ΄ μμ±μ μ΅μ μ λλ€.
// β κΈ°μ‘΄ λ°©μ const arr = [] for (let i = 0; i < 10; i++) { arr.push(0) } // λλ const arr2 = new Array(10).fill(0) // β Array.from() μ¬μ© const arr = Array.from({ length: 10 }, () => 0) // [0, 0, 0, ...] const arr2 = Array.from({ length: 10 }, (_, i) => i) // [0, 1, 2, ..., 9] // μ€μ μμ: 2μ°¨μ λ°°μ΄ μ΄κΈ°ν function create2DArray(rows, cols, defaultValue = 0) { return Array.from({ length: rows }, () => Array.from({ length: cols }, () => defaultValue)) } // μ€μ μμ: μμ°¨ λ°°μ΄ μμ± function range(start, end) { return Array.from({ length: end - start }, (_, i) => start + i) } // μ€μ μμ: νΉμ ν¨ν΄ λ°°μ΄ function createPattern(length, patternFn) { return Array.from({ length }, (_, i) => patternFn(i)) } // μ€μ μμ: μΈλ±μ€ κΈ°λ° μ΄κΈ°ν const matrix = Array.from({ length: 5 }, (_, i) => Array.from({ length: 5 }, (_, j) => i * 5 + j))- μ£Όμμ :
- λ λ²μ§Έ μΈμ(mapFn)λ₯Ό μλ΅νλ©΄
undefinedλ‘ μ±μμ§ λ°°μ΄μ΄ μμ±λ©λλ€. new Array(n).fill()κ³Ό λ¬λ¦¬ κ° μμλ₯Ό λ 립μ μΌλ‘ μ΄κΈ°νν μ μμ΅λλ€.
- λ λ²μ§Έ μΈμ(mapFn)λ₯Ό μλ΅νλ©΄
- κ°μ μ : λ°°μ΄ μ΄κΈ°νκ° λ μ μ°νκ³ ν¨μν νλ‘κ·Έλλ° μ€νμΌμ μ ν©ν©λλ€.
Array.prototype.includes()- μ€λͺ : λ°°μ΄μ νΉμ μμκ° ν¬ν¨λμ΄ μλμ§ νμΈν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: μ‘΄μ¬ μ¬λΆ 체ν¬, μ€λ³΅ κ²μ¬, νν°λ§ λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β κΈ°μ‘΄ λ°©μ function contains(arr, item) { for (let i = 0; i < arr.length; i++) { if (arr[i] === item) return true } return false } // λλ arr.indexOf(item) !== -1 // β includes() μ¬μ© const arr = [1, 2, 3, 4, 5] arr.includes(3) // true arr.includes(6) // false arr.includes(2, 3) // false (μΈλ±μ€ 3λΆν° κ²μ) // μ€μ μμ: μ‘΄μ¬ μ¬λΆ μ²΄ν¬ function isValid(value, allowedValues) { return allowedValues.includes(value) } // μ€μ μμ: μ€λ³΅ μ²΄ν¬ function addIfNotExists(arr, item) { if (!arr.includes(item)) { arr.push(item) } return arr } // μ€μ μμ: νν°λ§ function filterByValues(arr, values) { return arr.filter((item) => values.includes(item)) } // μ€μ μμ: λ¬Έμμ΄ ν¬ν¨ μ²΄ν¬ function hasVowel(str) { return ['a', 'e', 'i', 'o', 'u'].some((v) => str.includes(v)) }- μ£Όμμ :
indexOf()μ λ¬λ¦¬NaNλ μ¬λ°λ₯΄κ² μ²λ¦¬ν©λλ€ ([NaN].includes(NaN)= true).- λ λ²μ§Έ μΈμλ‘ μμ μΈλ±μ€λ₯Ό μ§μ ν μ μμ΅λλ€.
- κ°μ μ :
indexOf() !== -1λ³΄λ€ κ°λ μ±μ΄ μ’κ³ μ§κ΄μ μ λλ€.
Array.prototype.slice()/String.prototype.slice()- μ€λͺ : λ°°μ΄μ΄λ λ¬Έμμ΄μ μΌλΆλ₯Ό μΆμΆν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: λΆλΆ λ°°μ΄/λ¬Έμμ΄ μΆμΆ, λ³΅μ¬ λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β slice() μ¬μ© const arr = [1, 2, 3, 4, 5] arr.slice(1, 3) // [2, 3] (μΈλ±μ€ 1λΆν° 3 μ κΉμ§) arr.slice(2) // [3, 4, 5] (μΈλ±μ€ 2λΆν° λκΉμ§) arr.slice() // [1, 2, 3, 4, 5] (μ 체 볡μ¬) arr.slice(-2) // [4, 5] (λ€μμ 2κ°) const str = 'hello' str.slice(1, 3) // 'el' str.slice(2) // 'llo' str.slice(-2) // 'lo' // μ€μ μμ: λ°°μ΄ λ³΅μ¬ function copyArray(arr) { return arr.slice() } // μ€μ μμ: λΆλΆ λ°°μ΄ μΆμΆ function getSubarray(arr, start, end) { return arr.slice(start, end) } // μ€μ μμ: λ¬Έμμ΄ λΆλΆ μΆμΆ function getSubstring(str, start, end) { return str.slice(start, end) } // μ€μ μμ: λ§μ§λ§ Nκ° μμ function getLastN(arr, n) { return arr.slice(-n) } // μ€μ μμ: 첫 Nκ° μμ μ μΈ function skipFirstN(arr, n) { return arr.slice(n) }- μ£Όμμ :
slice()λ μλ³Έμ λ³κ²½νμ§ μμ΅λλ€ (splice()μ λ€λ¦).- μμ μΈλ±μ€λ λ°°μ΄/λ¬Έμμ΄ λμμλΆν° κ³μ°λ©λλ€.
- λ λ²μ§Έ μΈμλ₯Ό μλ΅νλ©΄ λκΉμ§ μΆμΆν©λλ€.
- κ°μ μ : λ°°μ΄/λ¬Έμμ΄μ λΆλΆ μΆμΆμ΄ κ°λ¨νκ³ μμ ν©λλ€.
1.4 String κ΄λ ¨ ν¨μλ€
String.prototype.split()/Array.prototype.join()- μ€λͺ
: μ€μ²© λ°λ³΅λ¬Έμ ν λ²μ
breakνκ±°λcontinueν λ μ¬μ©ν©λλ€. - μκ³ λ¦¬μ¦ νμ©: μ΄μ€/μΌμ€ 루νμμ νΉμ 쑰건 λ§μ‘± μ λͺ¨λ 루νλ₯Ό λΉ μ Έλμ¬ λ μ μ©ν©λλ€.
// β κΈ°μ‘΄ λ°©μ (flag λ³μ μ¬μ©) let found = false for (let i = 0; i < n; i++) { for (let j = 0; j < m; j++) { if (condition) { found = true break } } if (found) break } // β Label μ¬μ© outerLoop: for (let i = 0; i < n; i++) { for (let j = 0; j < m; j++) { if (condition) { break outerLoop // λͺ¨λ 루ν μ’ λ£ } } } // μ€μ μμ: 2D λ°°μ΄ νμ function findIn2D(grid, target) { searchLoop: for (let i = 0; i < grid.length; i++) { for (let j = 0; j < grid[i].length; j++) { if (grid[i][j] === target) { return [i, j] } if (grid[i][j] > target) { break searchLoop // μ λ ¬λ λ°°μ΄μμ μ‘°κΈ° μ’ λ£ } } } return null } // μ€μ μμ: continueλ‘ νΉμ 루ν κ³μ outer: for (let i = 0; i < 10; i++) { for (let j = 0; j < 10; j++) { if (j === 5) { continue outer // μΈλΆ 루νμ λ€μ λ°λ³΅μΌλ‘ } } }- μ£Όμμ :
- Label μ΄λ¦μ μλ³μ κ·μΉμ λ°λΌμΌ νλ©°, μ½λ‘ (
:)μΌλ‘ λλμΌ ν©λλ€. - λ¨μ©νλ©΄ μ½λ κ°λ μ±μ΄ λ¨μ΄μ§ μ μμΌλ―λ‘ νμν κ²½μ°μλ§ μ¬μ©ν©λλ€.
- Label μ΄λ¦μ μλ³μ κ·μΉμ λ°λΌμΌ νλ©°, μ½λ‘ (
- κ°μ μ : flag λ³μ μμ΄ κΉλνκ² μ€μ²© 루νλ₯Ό μ μ΄ν μ μμ΅λλ€.
- μ€λͺ
: μ€μ²© λ°λ³΅λ¬Έμ ν λ²μ
Math.max()/Math.min()- μ€λͺ : μ¬λ¬ κ° μ€ μ΅λκ°/μ΅μκ°μ λ°νν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: λ°°μ΄μ μ΅λ/μ΅μκ° μ°ΎκΈ°, λ κ° λΉκ΅, κ²½κ³κ° μ²΄ν¬ λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β κΈ°μ‘΄ λ°©μ function findMax(arr) { let max = arr[0] for (let i = 1; i < arr.length; i++) { if (arr[i] > max) max = arr[i] } return max } // β Math.max() μ¬μ© const max = Math.max(1, 5, 3, 9, 2) // 9 const maxFromArray = Math.max(...arr) // λ°°μ΄μ μ΅λκ° const min = Math.min(1, 5, 3, 9, 2) // 1 // μ€μ μμ: λ°°μ΄ μ΅λ/μ΅μκ° function getMaxMin(arr) { return { max: Math.max(...arr), min: Math.min(...arr) } } // μ€μ μμ: κ²½κ³κ° μ ν function clamp(value, min, max) { return Math.min(Math.max(value, min), max) } // μ€μ μμ: λ κ° μ€ ν°/μμ κ° μ ν function maxPath(a, b) { return Math.max(a, b) } // μ€μ μμ: μ¬λ¬ κ²½λ‘ μ€ μ΅λ¨ 거리 function shortestPath(paths) { return Math.min(...paths) }- μ£Όμμ :
- μΈμκ° μμΌλ©΄
Math.max()λ-Infinity,Math.min()μInfinityλ₯Ό λ°νν©λλ€. - λ°°μ΄μ μ§μ λ°μ§ μμΌλ―λ‘ μ€νλ λ μ°μ°μ(
...)λ₯Ό μ¬μ©ν΄μΌ ν©λλ€.
- μΈμκ° μμΌλ©΄
- κ°μ μ : μ΅λ/μ΅μκ° κ³μ°μ΄ κ°κ²°νκ³ κ°λ μ±μ΄ μ’μ΅λλ€.
Math.abs()- μ€λͺ : μ«μμ μ λκ°μ λ°νν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: 거리 κ³μ°, μ°¨μ΄κ° κ³μ°, μμ μ²λ¦¬ λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β κΈ°μ‘΄ λ°©μ function abs(n) { return n < 0 ? -n : n } // β Math.abs() μ¬μ© Math.abs(-5) // 5 Math.abs(5) // 5 Math.abs(0) // 0 // μ€μ μμ: λ μ μ¬μ΄ 거리 (μ λκ°) function manhattanDistance(x1, y1, x2, y2) { return Math.abs(x2 - x1) + Math.abs(y2 - y1) } // μ€μ μμ: μ°¨μ΄κ° κ³μ° function difference(a, b) { return Math.abs(a - b) } // μ€μ μμ: μμ λ°©μ§ function ensurePositive(n) { return Math.abs(n) } // μ€μ μμ: λ°°μ΄ μμ μ°¨μ΄μ μ΅λκ° function maxDifference(arr) { return Math.max(...arr) - Math.min(...arr) }- μ£Όμμ :
Math.abs(-0)μ0μ λ°νν©λλ€ (λΆνΈ μλ 0 μ²λ¦¬).NaNμ μ λ ₯νλ©΄NaNμ λ°νν©λλ€.
- κ°μ μ : μ λκ° κ³μ°μ΄ κ°λ¨νκ³ λͺ νν©λλ€.
Setμ νμ©ν μ€λ³΅ μ κ±°
- μ€λͺ : Set μλ£κ΅¬μ‘°λ₯Ό μ¬μ©νμ¬ λ°°μ΄μ μ€λ³΅μ μ κ±°ν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: μ€λ³΅ 체ν¬, μ λν¬ κ° μΆμΆ, μ§ν© μ°μ° λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β κΈ°μ‘΄ λ°©μ function removeDuplicates(arr) { const seen = {} const result = [] for (const item of arr) { if (!seen[item]) { seen[item] = true result.push(item) } } return result } // β Set μ¬μ© const arr = [1, 2, 2, 3, 3, 3, 4] const unique = [...new Set(arr)] // [1, 2, 3, 4] const uniqueSet = new Set(arr) // Set {1, 2, 3, 4} // μ€μ μμ: μ€λ³΅ μ κ±° function getUnique(arr) { return [...new Set(arr)] } // μ€μ μμ: μ€λ³΅ μ²΄ν¬ function hasDuplicates(arr) { return arr.length !== new Set(arr).size } // μ€μ μμ: λ λ°°μ΄μ κ³΅ν΅ μμ function intersection(arr1, arr2) { const set2 = new Set(arr2) return [...new Set(arr1)].filter((x) => set2.has(x)) } // μ€μ μμ: λ¬Έμμ΄ μ€λ³΅ μ κ±° function uniqueChars(str) { return [...new Set(str)].join('') }- μ£Όμμ :
- Setμ κ°μ²΄ μ°Έμ‘°λ₯Ό κΈ°μ€μΌλ‘ λΉκ΅νλ―λ‘, κ°μ²΄ λ°°μ΄μμλ μ£Όμκ° νμν©λλ€.
NaNμ Setμμλ μ€λ³΅μΌλ‘ μ²λ¦¬λ©λλ€ (NaN === NaNμ falseμ§λ§ Setμμλ νλλ§ μ μ₯).
- κ°μ μ : μ€λ³΅ μ κ±°κ° κ°κ²°νκ³ μ±λ₯λ μ’μ΅λλ€ (O(n) μκ° λ³΅μ‘λ).
Mapμλ£κ΅¬μ‘° νμ©- μ€λͺ : ν€-κ° μμ μ μ₯νλ μλ£κ΅¬μ‘°λ‘, κ°μ²΄λ³΄λ€ λ€μν ν€ νμ μ μ§μν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: ν΄μ λ§΅, λΉλμ μΉ΄μ΄νΈ, μΊμ± λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β κ°μ²΄μ νκ³ const obj = {} obj[1] = 'one' // ν€κ° λ¬Έμμ΄λ‘ λ³νλ¨ obj[[1, 2]] = 'array' // '[1,2]'λ‘ λ³νλ¨ // β Map μ¬μ© const map = new Map() map.set(1, 'one') map.set([1, 2], 'array') // λ°°μ΄λ ν€λ‘ μ¬μ© κ°λ₯ map.set('key', 'value') map.get(1) // 'one' map.has(1) // true map.size // 3 // μ€μ μμ: λΉλμ μΉ΄μ΄νΈ function countFrequency(arr) { const freq = new Map() for (const item of arr) { freq.set(item, (freq.get(item) || 0) + 1) } return freq } // μ€μ μμ: μΊμ± (λ©λͺ¨μ΄μ μ΄μ ) const cache = new Map() function fibonacci(n) { if (cache.has(n)) return cache.get(n) const result = n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2) cache.set(n, result) return result } // μ€μ μμ: κ·Έλ£Ήν function groupBy(arr, keyFn) { const map = new Map() for (const item of arr) { const key = keyFn(item) if (!map.has(key)) map.set(key, []) map.get(key).push(item) } return map } // μ€μ μμ: μν for (const [key, value] of map) { console.log(key, value) }- μ£Όμμ :
- κ°μ²΄μ λ¬λ¦¬
sizeμμ±μΌλ‘ ν¬κΈ°λ₯Ό λ°λ‘ μ μ μμ΅λλ€. - ν€λ‘
NaNλ μ¬μ© κ°λ₯νλ©°,NaN === NaNμ falseμ§λ§ Mapμμλ κ°μ ν€λ‘ μ²λ¦¬λ©λλ€. - κ°μ²΄λ³΄λ€ λ©λͺ¨λ¦¬ μ¬μ©λμ΄ μ½κ° λ λ§μ μ μμ΅λλ€.
- κ°μ²΄μ λ¬λ¦¬
- κ°μ μ : λ€μν ν€ νμ μ§μκ³Ό λͺ νν APIλ‘ ν΄μ λ§΅ ꡬνμ μ΅μ μ λλ€.
Object.keys()/Object.values()/Object.entries()- μ€λͺ : κ°μ²΄μ ν€, κ°, ν€-κ° μμ λ°°μ΄λ‘ λ°νν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: κ°μ²΄ μν, ν΄μ λ§΅ λ³ν, κ°μ²΄ μ‘°μ λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β κΈ°μ‘΄ λ°©μ const obj = { a: 1, b: 2, c: 3 } const keys = [] for (const key in obj) { if (obj.hasOwnProperty(key)) { keys.push(key) } } // β Object λ©μλ μ¬μ© const obj = { a: 1, b: 2, c: 3 } const keys = Object.keys(obj) // ['a', 'b', 'c'] const values = Object.values(obj) // [1, 2, 3] const entries = Object.entries(obj) // [['a', 1], ['b', 2], ['c', 3]] // μ€μ μμ: κ°μ²΄ μν function sumValues(obj) { return Object.values(obj).reduce((sum, val) => sum + val, 0) } // μ€μ μμ: κ°μ²΄ νν°λ§ function filterObject(obj, predicate) { return Object.fromEntries(Object.entries(obj).filter(([k, v]) => predicate(k, v))) } // μ€μ μμ: κ°μ²΄ λ³ν function invertObject(obj) { return Object.fromEntries(Object.entries(obj).map(([k, v]) => [v, k])) } // μ€μ μμ: κ°μ²΄ ν¬κΈ° function objectSize(obj) { return Object.keys(obj).length } // μ€μ μμ: κ°μ²΄ λ³ν© function mergeObjects(...objs) { return Object.assign({}, ...objs) }- μ£Όμμ :
Object.keys()λ μ΄κ±° κ°λ₯ν μμ±λ§ λ°νν©λλ€.- Symbol ν€λ ν¬ν¨λμ§ μμ΅λλ€ (Symbol ν€λ
Object.getOwnPropertySymbols()μ¬μ©).
- κ°μ μ : κ°μ²΄λ₯Ό λ°°μ΄μ²λΌ λ€λ£° μ μμ΄ ν¨μν νλ‘κ·Έλλ° μ€νμΌμ μ ν©ν©λλ€.
1.6 μ°μ°μ
μ€νλ λ μ°μ°μ (
...)- μ€λͺ : λ°°μ΄μ΄λ κ°μ²΄λ₯Ό νΌμ³μ κ°λ³ μμλ‘ νμ₯ν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: λ°°μ΄ λ³΅μ¬, λ³ν©, ν¨μ μΈμ μ λ¬ λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β μ€νλ λ μ°μ°μ μ¬μ© const arr1 = [1, 2, 3] const arr2 = [4, 5, 6] const merged = [...arr1, ...arr2] // [1, 2, 3, 4, 5, 6] const copied = [...arr1] // [1, 2, 3] (μμ 볡μ¬) // μ€μ μμ: λ°°μ΄ λ³΅μ¬ function copyArray(arr) { return [...arr] } // μ€μ μμ: λ°°μ΄ λ³ν© function mergeArrays(...arrays) { return [].concat(...arrays) } // μ€μ μμ: Math.max/minμ λ°°μ΄ μ λ¬ const max = Math.max(...arr) // arrμ μ΅λκ° const min = Math.min(...arr) // arrμ μ΅μκ° // μ€μ μμ: κ°μ²΄ λ³΅μ¬ λ° λ³ν© const obj1 = { a: 1, b: 2 } const obj2 = { ...obj1, c: 3 } // { a: 1, b: 2, c: 3 } // μ€μ μμ: ν¨μ μΈμ μ λ¬ function sum(a, b, c) { return a + b + c } const nums = [1, 2, 3] sum(...nums) // 6- μ£Όμμ :
- λ°°μ΄μ μμ 볡μ¬λ§ μννλ―λ‘, μ€μ²©λ λ°°μ΄/κ°μ²΄λ μ°Έμ‘°κ° λ³΅μ¬λ©λλ€.
- κ°μ²΄ μ€νλ λλ ES2018(ES9)λΆν° μ§μλ©λλ€.
- κ°μ μ : λ°°μ΄/κ°μ²΄ μ‘°μμ΄ κ°κ²°νκ³ ν¨μν νλ‘κ·Έλλ° μ€νμΌμ μ ν©ν©λλ€.
λμ€νΈλμ²λ§ (Destructuring)
- μ€λͺ : λ°°μ΄μ΄λ κ°μ²΄μμ κ°μ μΆμΆνμ¬ λ³μμ ν λΉν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: λ³μ κ΅ν, λ°°μ΄/κ°μ²΄ μμ μΆμΆ, ν¨μ νλΌλ―Έν° λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β λ°°μ΄ λμ€νΈλμ²λ§ const arr = [1, 2, 3] const [first, second, third] = arr const [a, , c] = arr // λ λ²μ§Έ μμ 건λλ°κΈ° const [head, ...rest] = arr // 첫 μμμ λλ¨Έμ§ // μ€μ μμ: λ³μ κ΅ν let a = 1, b = 2 ;[a, b] = [b, a] // a=2, b=1 (μμ λ³μ λΆνμ!) // μ€μ μμ: ν¨μ λ°νκ° λΆν΄ function getMinMax(arr) { return [Math.min(...arr), Math.max(...arr)] } const [min, max] = getMinMax([1, 5, 3, 9, 2]) // β κ°μ²΄ λμ€νΈλμ²λ§ const obj = { x: 1, y: 2, z: 3 } const { x, y } = obj const { x: xCoord, y: yCoord } = obj // μ΄λ¦ λ³κ²½ // μ€μ μμ: ν¨μ νλΌλ―Έν° function distance({ x: x1, y: y1 }, { x: x2, y: y2 }) { return Math.hypot(x2 - x1, y2 - y1) } // μ€μ μμ: κΈ°λ³Έκ° μ€μ const { name = 'Unknown', age = 0 } = user- μ£Όμμ :
- λ°°μ΄ λμ€νΈλμ²λ§μ μμκ° μ€μν©λλ€.
- κ°μ²΄ λμ€νΈλμ²λ§μ μμ± μ΄λ¦μ΄ μΌμΉν΄μΌ ν©λλ€.
- μ€μ²©λ ꡬ쑰λ λμ€νΈλμ²λ§ κ°λ₯ν©λλ€.
- κ°μ μ : λ³μ ν λΉμ΄ κ°κ²°νκ³ κ°λ μ±μ΄ μ’μ΅λλ€.
λλ¨Έμ§ μ°μ°μ (
%) νμ©- μ€λͺ : λλμ μ λλ¨Έμ§λ₯Ό λ°νν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: νμ§ νλ³, μν μΈλ±μ€, λͺ¨λλ¬ μ°μ° λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β λλ¨Έμ§ μ°μ°μ νμ© 10 % 3 // 1 10 % 2 // 0 (μ§μ) 11 % 2 // 1 (νμ) // μ€μ μμ: νμ§ νλ³ function isEven(n) { return n % 2 === 0 } // μ€μ μμ: μν μΈλ±μ€ function circularIndex(i, length) { return i % length } const arr = [0, 1, 2, 3, 4] arr[10 % arr.length] // 0 (10λ²μ§Έλ 0λ²μ§Έμ κ°μ) // μ€μ μμ: λͺ¨λλ¬ μ°μ° (ν° μ μ²λ¦¬) function modPow(base, exp, mod) { let result = 1 base = base % mod while (exp > 0) { if (exp % 2 === 1) result = (result * base) % mod exp = Math.floor(exp / 2) base = (base * base) % mod } return result } // μ€μ μμ: λ°°μ μ²΄ν¬ function isMultiple(n, divisor) { return n % divisor === 0 }- μ£Όμμ :
- μμμ λν λλ¨Έμ§ μ°μ° κ²°κ³Όλ μΈμ΄λ§λ€ λ€λ₯Ό μ μμ§λ§, JavaScriptμμλ νΌμ μμ λΆνΈλ₯Ό λ°λ¦ λλ€.
-10 % 3μ-1μ λλ€ (Pythonκ³Ό λ€λ¦).
- κ°μ μ : μν ꡬ쑰λ λͺ¨λλ¬ μ°μ°μ΄ νμν μκ³ λ¦¬μ¦μμ νμμ μ λλ€.
λΉνΈ μ°μ°μ (
<<,>>,&,|,^)- μ€λͺ : λΉνΈ λ¨μ μ°μ°μ μνν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: λΉ λ₯Έ κ³±μ /λλμ , νλκ·Έ κ΄λ¦¬, μ§ν© νν λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β λΉνΈ μ°μ°μ νμ© 5 << 1 // 10 (2λ°°, 5 * 2) 10 >> 1 // 5 (μ λ°, 10 / 2) 5 & 3 // 1 (AND: 101 & 011 = 001) 5 | 3 // 7 (OR: 101 | 011 = 111) 5 ^ 3 // 6 (XOR: 101 ^ 011 = 110) // μ€μ μμ: λΉ λ₯Έ 2μ κ±°λμ κ³± function powerOfTwo(n) { return 1 << n // 2^n } // μ€μ μμ: μ§ν© νν (λΉνΈλ§μ€ν¬) const SET_A = 1 << 0 // 1 (0001) const SET_B = 1 << 1 // 2 (0010) const SET_C = 1 << 2 // 4 (0100) const combined = SET_A | SET_C // 5 (0101, Aμ C ν¬ν¨) // μ€μ μμ: μ§ν© ν¬ν¨ μ¬λΆ μ²΄ν¬ function hasSet(flags, set) { return (flags & set) !== 0 } // μ€μ μμ: νμ§ νλ³ (λΉ λ₯Έ λ°©λ²) function isOdd(n) { return (n & 1) === 1 } // μ€μ μμ: 2μ κ±°λμ κ³± μ²΄ν¬ function isPowerOfTwo(n) { return n > 0 && (n & (n - 1)) === 0 }- μ£Όμμ :
- λΉνΈ μ°μ°μ 32λΉνΈ μ μλ‘ λ³νλμ΄ μνλ©λλ€.
>>>(λΆνΈ μλ μ€λ₯Έμͺ½ μννΈ)λ μμ§λ§ μΌλ°μ μΌλ‘λ>>λ₯Ό μ¬μ©ν©λλ€.- κ°λ μ±μ΄ λ¨μ΄μ§ μ μμΌλ―λ‘ μ£Όμμ μΆκ°νλ κ²μ΄ μ’μ΅λλ€.
- κ°μ μ : μ±λ₯ μ΅μ νκ° νμν κ²½μ°λ λΉνΈλ§μ€ν¬λ₯Ό μ¬μ©ν μ§ν© ννμ μ μ©ν©λλ€.
String.prototype.slice()- μ€λͺ : λ¬Έμμ΄μ μΌλΆλ₯Ό μΆμΆν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: λΆλΆ λ¬Έμμ΄ μΆμΆ, λ³΅μ¬ λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β slice() μ¬μ© const str = 'hello world' str.slice(0, 5) // 'hello' str.slice(6) // 'world' str.slice(-5) // 'world' str.slice(0, -6) // 'hello' // μ€μ μμ: λΆλΆ λ¬Έμμ΄ μΆμΆ function getSubstring(str, start, end) { return str.slice(start, end) } // μ€μ μμ: λ§μ§λ§ Nκ° λ¬Έμ function getLastNChars(str, n) { return str.slice(-n) }- μ£Όμμ :
slice()λ μλ³Έμ λ³κ²½νμ§ μμ΅λλ€.- μμ μΈλ±μ€λ λ¬Έμμ΄ λμμλΆν° κ³μ°λ©λλ€.
- κ°μ μ : λ¬Έμμ΄μ λΆλΆ μΆμΆμ΄ κ°λ¨νκ³ μμ ν©λλ€.
μ κ·ννμ κΈ°λ³Έ νμ©
- μ€λͺ : ν¨ν΄ λ§€μΉμ μν μ κ·ννμμ κΈ°λ³Έ μ¬μ©λ²μ λλ€.
- μκ³ λ¦¬μ¦ νμ©: λ¬Έμμ΄ κ²μ¦, ν¨ν΄ λ§€μΉ, μΉν λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β μ κ·ννμ κΈ°λ³Έ μ¬μ© const regex = /\d+/g // μ«μ νλ μ΄μ (μ μ κ²μ) const str = 'abc123def456' str.match(regex) // ['123', '456'] regex.test(str) // true // μ€μ μμ: μ«μ μΆμΆ function extractNumbers(str) { return str.match(/\d+/g) || [] } // μ€μ μμ: μ΄λ©μΌ κ²μ¦ function isValidEmail(email) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email) } // μ€μ μμ: μ«μλ§ μ²΄ν¬ function isNumeric(str) { return /^\d+$/.test(str) } // μ€μ μμ: μνλ²³λ§ μ²΄ν¬ function isAlpha(str) { return /^[a-zA-Z]+$/.test(str) } // μ€μ μμ: 곡백 μ κ±° function removeSpaces(str) { return str.replace(/\s+/g, '') } // μ€μ μμ: νΉμλ¬Έμ μ κ±° function removeSpecialChars(str) { return str.replace(/[^a-zA-Z0-9]/g, '') } // μ€μ μμ: ν¨ν΄ λ§€μΉ function findPattern(str, pattern) { const regex = new RegExp(pattern, 'g') return str.match(regex) || [] }- μ£Όμμ :
test()λ booleanμ λ°ννκ³ ,match()λ λ°°μ΄μ λ°νν©λλ€.- μ μ νλκ·Έ(
g)λ₯Ό μ¬μ©νλ©΄match()λ λͺ¨λ λ§€μΉμ λ°νν©λλ€. ^λ μμ,$λ λμ μλ―Έν©λλ€.
- κ°μ μ : 볡μ‘ν λ¬Έμμ΄ ν¨ν΄ κ²μ¦μ΄ κ°κ²°νκ² κ°λ₯ν©λλ€.
1.5 μλ£κ΅¬μ‘°
Setμ νμ©ν μ€λ³΅ μ κ±°
- μ€λͺ : ν€-κ° μμ μ μ₯νλ μλ£κ΅¬μ‘°λ‘, κ°μ²΄λ³΄λ€ λ€μν ν€ νμ μ μ§μν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: ν΄μ λ§΅, λΉλμ μΉ΄μ΄νΈ, μΊμ± λ±μ μμ£Ό μ¬μ©λ©λλ€.
// β κ°μ²΄μ νκ³ const obj = {} obj[1] = 'one' // ν€κ° λ¬Έμμ΄λ‘ λ³νλ¨ obj[[1, 2]] = 'array' // '[1,2]'λ‘ λ³νλ¨ // β Map μ¬μ© const map = new Map() map.set(1, 'one') map.set([1, 2], 'array') // λ°°μ΄λ ν€λ‘ μ¬μ© κ°λ₯ map.set('key', 'value') map.get(1) // 'one' map.has(1) // true map.size // 3 // μ€μ μμ: λΉλμ μΉ΄μ΄νΈ function countFrequency(arr) { const freq = new Map() for (const item of arr) { freq.set(item, (freq.get(item) || 0) + 1) } return freq } // μ€μ μμ: μΊμ± (λ©λͺ¨μ΄μ μ΄μ ) const cache = new Map() function fibonacci(n) { if (cache.has(n)) return cache.get(n) const result = n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2) cache.set(n, result) return result } // μ€μ μμ: κ·Έλ£Ήν function groupBy(arr, keyFn) { const map = new Map() for (const item of arr) { const key = keyFn(item) if (!map.has(key)) map.set(key, []) map.get(key).push(item) } return map } // μ€μ μμ: μν for (const [key, value] of map) { console.log(key, value) }- μ£Όμμ :
- κ°μ²΄μ λ¬λ¦¬
sizeμμ±μΌλ‘ ν¬κΈ°λ₯Ό λ°λ‘ μ μ μμ΅λλ€. - ν€λ‘
NaNλ μ¬μ© κ°λ₯νλ©°,NaN === NaNμ falseμ§λ§ Mapμμλ κ°μ ν€λ‘ μ²λ¦¬λ©λλ€. - κ°μ²΄λ³΄λ€ λ©λͺ¨λ¦¬ μ¬μ©λμ΄ μ½κ° λ λ§μ μ μμ΅λλ€.
- κ°μ²΄μ λ¬λ¦¬
- κ°μ μ : λ€μν ν€ νμ μ§μκ³Ό λͺ νν APIλ‘ ν΄μ λ§΅ ꡬνμ μ΅μ μ λλ€.
1.7 κΈ°ν
Label (loop:)
- μ€λͺ
: μ€μ²© λ°λ³΅λ¬Έμ ν λ²μ
breakνκ±°λcontinueν λ μ¬μ©ν©λλ€. - μκ³ λ¦¬μ¦ νμ©: μ΄μ€/μΌμ€ 루νμμ νΉμ 쑰건 λ§μ‘± μ λͺ¨λ 루νλ₯Ό λΉ μ Έλμ¬ λ μ μ©ν©λλ€.
// β κΈ°μ‘΄ λ°©μ (flag λ³μ μ¬μ©) let found = false for (let i = 0; i < n; i++) { for (let j = 0; j < m; j++) { if (condition) { found = true break } } if (found) break } // β Label μ¬μ© outerLoop: for (let i = 0; i < n; i++) { for (let j = 0; j < m; j++) { if (condition) { break outerLoop // λͺ¨λ 루ν μ’ λ£ } } } // μ€μ μμ: 2D λ°°μ΄ νμ function findIn2D(grid, target) { searchLoop: for (let i = 0; i < grid.length; i++) { for (let j = 0; j < grid[i].length; j++) { if (grid[i][j] === target) { return [i, j] } if (grid[i][j] > target) { break searchLoop // μ λ ¬λ λ°°μ΄μμ μ‘°κΈ° μ’ λ£ } } } return null } // μ€μ μμ: continueλ‘ νΉμ 루ν κ³μ outer: for (let i = 0; i < 10; i++) { for (let j = 0; j < 10; j++) { if (j === 5) { continue outer // μΈλΆ 루νμ λ€μ λ°λ³΅μΌλ‘ } } }- μ£Όμμ :
- Label μ΄λ¦μ μλ³μ κ·μΉμ λ°λΌμΌ νλ©°, μ½λ‘ (
:)μΌλ‘ λλμΌ ν©λλ€. - λ¨μ©νλ©΄ μ½λ κ°λ μ±μ΄ λ¨μ΄μ§ μ μμΌλ―λ‘ νμν κ²½μ°μλ§ μ¬μ©ν©λλ€.
- Label μ΄λ¦μ μλ³μ κ·μΉμ λ°λΌμΌ νλ©°, μ½λ‘ (
- κ°μ μ : flag λ³μ μμ΄ κΉλνκ² μ€μ²© 루νλ₯Ό μ μ΄ν μ μμ΅λλ€.
- μ€λͺ
: μ€μ²© λ°λ³΅λ¬Έμ ν λ²μ
2. ES2020 (ES11): μμ ν νμκ³Ό ν° μ μ²λ¦¬
Node.js μ§μ: Node.js 14.0.0+ (2020λ 4μ)
κ°μ₯ λ²μ©μ μΌλ‘ μ¬μ©λλ©°, λ°νμ μλ¬λ₯Ό λ°©μ§νλ ν΅μ¬ λ¬Έλ²μ λλ€.
Optional Chaining (
?.)- μ€λͺ
:
nullλλundefinedμΌ μ μλ κ°μ²΄ μμ±μ μμ νκ² μ κ·Όν©λλ€. - μκ³ λ¦¬μ¦ νμ©: 2μ°¨μ λ°°μ΄ νμ μ μΈλ±μ€ λ²μλ₯Ό 체ν¬νλ λ²κ±°λ‘μ΄
ifλ¬Έμ μ€μ¬μ€λλ€. (grid[r]?.[c])
// β κΈ°μ‘΄ λ°©μ function getValue(grid, r, c) { if (grid && grid[r] && grid[r][c] !== undefined) { return grid[r][c] } return null } // β Optional Chaining μ¬μ© function getValue(grid, r, c) { return grid?.[r]?.[c] ?? null } // μ€μ μμ: 2D λ°°μ΄ κ²½λ‘ νμ const grid = [ [1, 2], [3, 4], ] const value = grid?.[5]?.[2] // undefined (μλ¬ μμ)- μ£Όμμ :
?.λnull/undefinedμμλ§ λ©μΆκ³ ,0,false,""κ°μ falsy κ°μμλ κ³μ μ§νν©λλ€.- λ©μλ νΈμΆ μ
obj.method?.()ννλ‘ μ¬μ©ν΄μΌ ν©λλ€.
- κ°μ μ : μ€μ²©λ κ°μ²΄ μ κ·Ό μ κ°λ μ±κ³Ό μμ μ±μ΄ ν¬κ² ν₯μλ©λλ€.
- μ€λͺ
:
Nullish Coalescing (
??)- μ€λͺ
: κ°μ΄
nullμ΄λundefinedμΌ λλ§ κΈ°λ³Έκ°μ λΆμ¬ν©λλ€. - μκ³ λ¦¬μ¦ νμ©:
0μ΄λ""μ΄ μ ν¨ν μ λ΅μΌ μ μλ λ¬Έμ μμ||μ°μ°μμ λΆμμ©μ λ°©μ§ν©λλ€.
// β || μ°μ°μμ λ¬Έμ μ const count = 0 const result = count || 10 // 10 (0μ falsyμ΄λ―λ‘) // β ?? μ°μ°μ μ¬μ© const count = 0 const result = count ?? 10 // 0 (μ ν¨ν κ°μΌλ‘ μ²λ¦¬) // μ€μ μμ: λ°°μ΄ ν©κ³ κ³μ° function sumArray(arr) { return arr.reduce((acc, val) => acc + (val ?? 0), 0) } // μ€μ μμ: κΈ°λ³Έκ° μ€μ const userInput = getUserInput() ?? 'default' const maxValue = findMax(nums) ?? -Infinity- μ£Όμμ :
??λnullκ³Όundefinedλ§ μ²΄ν¬νλ―λ‘,0,false,""λ μ ν¨ν κ°μΌλ‘ μ²λ¦¬λ©λλ€.||μ νΌμ©νμ§ μλλ‘ μ£Όμ (μ°μ°μ μ°μ μμ λ¬Έμ ).
- κ°μ μ : μ«μ 0μ΄λ λΉ λ¬Έμμ΄μ΄ μ ν¨ν κ°μΈ κ²½μ°μ μ νν κΈ°λ³Έκ° μ²λ¦¬κ° κ°λ₯ν©λλ€.
- μ€λͺ
: κ°μ΄
BigInt (
n)- μ€λͺ : μ μ μ λ°λ μ ν()μ λμ΄μ μ«μλ₯Ό μ²λ¦¬ν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: κ²°κ³Όκ°μ΄ λ§€μ° ν° μ‘°ν©(Combination)μ΄λ νΌλ³΄λμΉ μμ΄ κ³μ° μ νμμ λλ€.
// β Number νμ μ νκ³ const maxSafe = Number.MAX_SAFE_INTEGER // 9007199254740991 const overflow = maxSafe + 1 // μ λ°λ μμ€ λ°μ κ°λ₯ // β BigInt μ¬μ© const bigNum = 9007199254740992n const result = bigNum + 1n // μ νν κ³μ° // μ€μ μμ: ν° μ νΌλ³΄λμΉ function fibonacciBigInt(n) { if (n <= 1) return BigInt(n) let a = 0n, b = 1n for (let i = 2; i <= n; i++) { ;[a, b] = [b, a + b] } return b } // μ€μ μμ: μ‘°ν© κ³μ° (nCr) function combinationBigInt(n, r) { if (r > n) return 0n let result = 1n for (let i = 0; i < r; i++) { result = (result * BigInt(n - i)) / BigInt(i + 1) } return result }- μ£Όμμ :
- BigIntμ Numberλ μ§μ μ°μ°ν μ μμ΅λλ€. (
1n + 1β,1n + 1nβ ) - JSON.stringify()μμ BigIntλ μλ¬κ° λ°μνλ―λ‘ λ¬Έμμ΄λ‘ λ³ν νμ.
- λΉκ΅ μ°μ°μ κ°λ₯νμ§λ§ (
1n < 2β ), μΌλΆ Math λ©μλλ μ¬μ© λΆκ°.
- BigIntμ Numberλ μ§μ μ°μ°ν μ μμ΅λλ€. (
- κ°μ μ : λ§€μ° ν° μ μ κ³μ°μ΄ νμν μκ³ λ¦¬μ¦ λ¬Έμ μμ μ νν κ²°κ³Όλ₯Ό 보μ₯ν©λλ€.
Number.MAX_SAFE_INTEGER- μ€λͺ : JavaScriptμμ μμ νκ² ννν μ μλ μ΅λ μ μ κ° ()μ λλ€.
- μκ³ λ¦¬μ¦ νμ©: 무νλ κ° λμ μ¬μ©νκ±°λ, ν° μ κ³μ° μ μ λ°λ νκ³λ₯Ό νμΈν λ μ¬μ©ν©λλ€.
// β 무νλ κ° μ²λ¦¬ const INF = Number.MAX_SAFE_INTEGER // 9007199254740991 const MIN_INF = Number.MIN_SAFE_INTEGER // -9007199254740991 // μ€μ μμ: μ΅λ¨ κ²½λ‘ μκ³ λ¦¬μ¦ function dijkstra(graph, start) { const dist = new Array(graph.length).fill(Number.MAX_SAFE_INTEGER) dist[start] = 0 // ... } // μ€μ μμ: μ΅λκ° μ΄κΈ°ν function findMaxPath(grid) { let maxPath = -Number.MAX_SAFE_INTEGER // ... } // μ€μ μμ: μ λ°λ μ²΄ν¬ function isSafeNumber(n) { return n <= Number.MAX_SAFE_INTEGER && n >= Number.MIN_SAFE_INTEGER }- μ£Όμμ :
- μ΄ κ°μ λμ΄μλ©΄ μ λ°λ μμ€μ΄ λ°μν μ μμ΅λλ€.
- λ ν° μκ° νμνλ©΄ BigIntλ₯Ό μ¬μ©ν΄μΌ ν©λλ€.
- κ°μ μ : 무νλλ₯Ό ννν λ
Infinityλμ μ¬μ©νλ©΄ λΉκ΅ μ°μ°μ΄ λ μμ ν©λλ€.
3. ES2021 (ES12): λ¬Έμμ΄ μ μ μ κ°λ μ±
Node.js μ§μ: Node.js 15.0.0+ (2020λ 10μ)
String.prototype.replaceAll()- μ€λͺ : μ κ·μ μμ΄ λͺ¨λ μΌμΉ νλͺ©μ μΉνν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: λ¬Έμμ΄ μΉν λ° μ μ²λ¦¬ λ¬Έμ μμ ꡬν μλλ₯Ό λμ¬μ€λλ€.
// β κΈ°μ‘΄ λ°©μ function replaceAll(str, search, replace) { return str.split(search).join(replace) } // λλ μ κ·μ μ¬μ© str.replace(/old/g, 'new') // β replaceAll μ¬μ© const text = 'hello world hello' const result = text.replaceAll('hello', 'hi') // "hi world hi" // μ€μ μμ: λ¬Έμμ΄ μ μ function cleanString(str) { return str.replaceAll(' ', '').replaceAll('\n', '').replaceAll('\t', '') } // μ€μ μμ: ν¨ν΄ μΉν function maskPhone(phone) { return phone.replaceAll(/\d(?=\d{4})/g, '*') // λ§μ§λ§ 4μ리 μ μΈ λ§μ€νΉ }- μ£Όμμ :
- 첫 λ²μ§Έ μΈμλ λ¬Έμμ΄μ΄μ΄μΌ νλ©°, μ κ·μμ΄ μλλλ€.
- λμλ¬Έμλ₯Ό ꡬλΆνλ―λ‘ νμμ
toLowerCase()μ ν¨κ» μ¬μ©.
- κ°μ μ : μ κ·μ μμ΄ κ°λ¨νκ² λͺ¨λ μΌμΉ νλͺ©μ μΉνν μ μμ΄ κ°λ μ±μ΄ ν₯μλ©λλ€.
Numeric Separators (
_)- μ€λͺ
: ν° μ«μμ μΈλλ°λ₯Ό μ¬μ©νμ¬ μλ¦Ώμλ₯Ό ꡬλΆν©λλ€. (
1_000_000_000) - μκ³ λ¦¬μ¦ νμ©:
MAX_SAFE_INTEGERλ ν° μμλ₯Ό μ€μ ν λ0μ κ°μλ₯Ό μ€μνμ§ μκ² ν΄μ€λλ€.
// β κΈ°μ‘΄ λ°©μ (κ°λ μ± μ ν) const billion = 1000000000 const maxSafe = 9007199254740991 // β Numeric Separators μ¬μ© const billion = 1_000_000_000 const maxSafe = 9_007_199_254_740_991 // μ€μ μμ: μμ μ μ const MOD = 1_000_000_007 // λͺ¨λλ¬ μ°μ° μμ const INF = 1_000_000_000_000 // 무νλ λμ²΄κ° const MAX_N = 100_000 // μ΅λ μ λ ₯ ν¬κΈ° // λ€μν μ§λ²μμλ μ¬μ© κ°λ₯ const binary = 0b1010_0001 const hex = 0xff_ec_de_5e- μ£Όμμ :
- μΈλλ°λ μ«μ κ°μ μν₯μ μ£Όμ§ μμΌλ©°, μμνκ² κ°λ μ± ν₯μμ©μ λλ€.
- μ«μ μμμ΄λ λ, μμμ μλ€μλ μ¬μ©ν μ μμ΅λλ€.
- κ°μ μ : ν° μ«μμ κ°λ μ±μ΄ ν¬κ² ν₯μλμ΄ μ€μλ₯Ό μ€μΌ μ μμ΅λλ€.
- μ€λͺ
: ν° μ«μμ μΈλλ°λ₯Ό μ¬μ©νμ¬ μλ¦Ώμλ₯Ό ꡬλΆν©λλ€. (
4. ES2022 (ES13): λ°°μ΄ μ κ·Όμ νμ
Node.js μ§μ: Node.js 16.9.0+ (2021λ 9μ)
Array.prototype.at(index)- μ€λͺ : μμ μΈλ±μ€λ₯Ό μ§μν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: μ€ν(Stack)μ Top μμλ₯Ό νμΈνκ±°λ λ°°μ΄ λμ κ°μ μ°Έμ‘°ν λ
arr[arr.length - 1]λμarr.at(-1)λ‘ κ°λ μ±μ λμ λλ€.
// β κΈ°μ‘΄ λ°©μ const arr = [1, 2, 3, 4, 5] const last = arr[arr.length - 1] // 5 const secondLast = arr[arr.length - 2] // 4 // β at() μ¬μ© const last = arr.at(-1) // 5 const secondLast = arr.at(-2) // 4 const first = arr.at(0) // 1 (μμ μΈλ±μ€λ κ°λ₯) // μ€μ μμ: μ€ν ꡬν class Stack { constructor() { this.items = [] } push(item) { this.items.push(item) } pop() { return this.items.pop() } top() { return this.items.at(-1) } // λ§μ§λ§ μμ isEmpty() { return this.items.length === 0 } } // μ€μ μμ: μ¬λΌμ΄λ© μλμ° function findMaxInWindow(arr, k) { const result = [] for (let i = k - 1; i < arr.length; i++) { const window = arr.slice(i - k + 1, i + 1) result.push(Math.max(...window)) } return result }- μ£Όμμ :
- λ²μλ₯Ό λ²μ΄λ μΈλ±μ€λ
undefinedλ₯Ό λ°νν©λλ€ (arr.at(100)βundefined). - μμ μΈλ±μ€λ λ°°μ΄ λμμλΆν° κ³μ°λ©λλ€ (
-1= λ§μ§λ§ μμ).
- λ²μλ₯Ό λ²μ΄λ μΈλ±μ€λ
- κ°μ μ : λ°°μ΄ λ μμ μ κ·Όμ΄ λ μ§κ΄μ μ΄κ³ κ°λ μ±μ΄ ν₯μλ©λλ€.
Object.hasOwn()- μ€λͺ
:
hasOwnPropertyμ μμ ν μ μ λ©μλ λ²μ μ λλ€. - μκ³ λ¦¬μ¦ νμ©: ν΄μ λ§΅ ꡬν μ νλ‘ν νμ 체μΈμ κ°μ μμ΄ μμ± μ‘΄μ¬ μ¬λΆλ₯Ό νμΈν©λλ€.
// β κΈ°μ‘΄ λ°©μμ λ¬Έμ μ const obj = { a: 1 } obj.hasOwnProperty('a') // true // νμ§λ§ hasOwnPropertyκ° μ€λ²λΌμ΄λλλ©΄ λ¬Έμ λ°μ const obj2 = Object.create(null) obj2.hasOwnProperty('a') // TypeError! // β Object.hasOwn() μ¬μ© const obj = { a: 1 } Object.hasOwn(obj, 'a') // true Object.hasOwn(obj, 'toString') // false (νλ‘ν νμ μ²΄μΈ λ¬΄μ) // μ€μ μμ: ν΄μ λ§΅ ꡬν class HashMap { constructor() { this.data = {} } set(key, value) { this.data[key] = value } has(key) { return Object.hasOwn(this.data, key) } get(key) { return this.has(key) ? this.data[key] : undefined } } // μ€μ μμ: κ°μ²΄ μμ± μ²΄ν¬ function countProperties(obj) { return Object.keys(obj).filter((key) => Object.hasOwn(obj, key)).length }- μ£Όμμ :
- νλ‘ν νμ
체μΈμ νμΈνμ§ μμΌλ―λ‘, μμλ μμ±μ
falseλ₯Ό λ°νν©λλ€. Object.hasOwn()μ μ μ λ©μλμ΄λ―λ‘ κ°μ²΄ μΈμ€ν΄μ€μμ μ§μ νΈμΆν μ μμ΅λλ€.
- νλ‘ν νμ
체μΈμ νμΈνμ§ μμΌλ―λ‘, μμλ μμ±μ
- κ°μ μ : νλ‘ν νμ μ²΄μΈ κ°μ μμ΄ μμ νκ² μμ± μ‘΄μ¬ μ¬λΆλ₯Ό νμΈν μ μμ΅λλ€.
- μ€λͺ
:
structuredClone()(μ μ API)- μ€λͺ : λΈλΌμ°μ μ Node.jsμμ 곡μ μ§μνλ κΉμ 볡μ¬(Deep Copy) ν¨μμ λλ€.
- μκ³ λ¦¬μ¦ νμ©: 2μ°¨μ λ°°μ΄μ μλ³Έμ 보쑴νλ©° DFS/BFS νμ μνλ₯Ό λκΈΈ λ μ΅μ μ λλ€.
// β μμ 볡μ¬μ λ¬Έμ const arr = [ [1, 2], [3, 4], ] const shallow = [...arr] shallow[0][0] = 999 console.log(arr[0][0]) // 999 (μλ³Έλ λ³κ²½λ¨!) // β JSON λ°©μμ νκ³ const deep1 = JSON.parse(JSON.stringify(arr)) // ν¨μ, undefined λ± λ³΅μ¬ λΆκ° // β structuredClone() μ¬μ© const arr = [ [1, 2], [3, 4], ] const deep = structuredClone(arr) deep[0][0] = 999 console.log(arr[0][0]) // 1 (μλ³Έ μ μ§) // μ€μ μμ: DFS/BFS μν λ³΅μ¬ function dfs(grid, visited) { const newVisited = structuredClone(visited) // newVisitedλ₯Ό μμ ν΄λ μλ³Έ visitedλ μμ // ... } // μ€μ μμ: λ°±νΈλνΉ function backtrack(state, path) { const newState = structuredClone(state) const newPath = structuredClone(path) // μν 볡μ μμ΄ μμ νκ² νμ // ... } // μ€μ μμ: κ°μ²΄ κΉμ λ³΅μ¬ const complex = { arr: [1, 2, { nested: true }], map: new Map([['key', 'value']]), set: new Set([1, 2, 3]), } const cloned = structuredClone(complex)- μ£Όμμ :
- ν¨μ, Symbol, DOM λ Έλ λ±μ 볡μ¬ν μ μμ΅λλ€.
- μν μ°Έμ‘°κ° μμΌλ©΄ μλ¬κ° λ°μν©λλ€.
Map,Set,Dateλ±μ 볡μ¬λμ§λ§ μΌλΆ νμ μ μ νμ μ λλ€.
- κ°μ μ : JSON λ°©μλ³΄λ€ ν¨μ¬ λ§μ νμ μ μ§μνλ©°, μλ³Έ λ°μ΄ν°λ₯Ό μμ νκ² λ³΄μ‘΄ν μ μμ΅λλ€.
5. ES2023 (ES14): λΆλ³μ± λ°°μ΄ λ©μλ
Node.js μ§μ: Node.js 20.0.0+ (2023λ 4μ)
μλ³Έ λ°μ΄ν°λ₯Ό μμ (Mutate)νμ§ μκ³ 'κ°κ³΅λ μ λ°°μ΄'μ μ¦μ λ°ννμ¬ μ¬μ΄λ μ΄ννΈλ₯Ό λ°©μ§ν©λλ€.
toSorted(),toReversed()- μ€λͺ : μλ³Έμ μ μ§νλ©΄μ μ λ ¬/λ°μ λ μ λ°°μ΄μ λ°νν©λλ€.
- μκ³ λ¦¬μ¦ νμ©:
const sorted = [...arr].sort()νλ κ³Όμ μarr.toSorted()ν μ€λ‘ λ체ν©λλ€.
// β κΈ°μ‘΄ λ°©μ (μλ³Έ λ³κ²½) const arr = [3, 1, 4, 1, 5] const sorted = [...arr].sort() // λ³΅μ¬ ν μ λ ¬ arr.sort() // μλ³Έλ λ³κ²½λ¨! // β toSorted() μ¬μ© const arr = [3, 1, 4, 1, 5] const sorted = arr.toSorted() // [1, 1, 3, 4, 5] const reversed = arr.toReversed() // [5, 1, 4, 1, 3] console.log(arr) // [3, 1, 4, 1, 5] (μλ³Έ μ μ§) // μ€μ μμ: μ λ ¬λ λ°°μ΄ μμ± function findKthLargest(nums, k) { return nums.toSorted((a, b) => b - a)[k - 1] } // μ€μ μμ: μμ μ²λ¦¬ function reverseWords(s) { return s.split(' ').toReversed().join(' ') } // μ€μ μμ: λΆλ³μ± μ μ§ function processData(data) { const sorted = data.toSorted() const processed = sorted.map((x) => x * 2) return { original: data, processed } // μλ³Έ 보쑴 }- μ£Όμμ :
toSorted()λ λΉκ΅ ν¨μλ₯Ό μΈμλ‘ λ°μ μ μμ΅λλ€ (arr.toSorted((a, b) => a - b)).- μλ³Έ λ°°μ΄μ μ λ λ³κ²½λμ§ μμΌλ―λ‘, λ©λͺ¨λ¦¬ μ¬μ©λμ΄ μ¦κ°ν μ μμ΅λλ€.
- κ°μ μ : λΆλ³μ±μ 보μ₯νλ©΄μ μ½λκ° λ κ°κ²°ν΄μ§κ³ , μ€μλ‘ μλ³Έμ λ³κ²½νλ λ²κ·Έλ₯Ό λ°©μ§ν©λλ€.
with(index, value)- μ€λͺ : νΉμ μΈλ±μ€λ§ μμ λ μ λ°°μ΄μ λ°νν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: νΉμ λ Έλλ§ λ°©λ¬Έ νμνκ±°λ κ°μ λ°κΎΌ μνμ λ°°μ΄μ λ€μ μ¬κ· ν¨μλ‘ μ λ¬ν λ μ μ©ν©λλ€.
// β κΈ°μ‘΄ λ°©μ const arr = [1, 2, 3, 4, 5] const newArr = [...arr] newArr[2] = 999 // μ¬λ¬ μ€ νμ // β with() μ¬μ© const arr = [1, 2, 3, 4, 5] const newArr = arr.with(2, 999) // [1, 2, 999, 4, 5] console.log(arr) // [1, 2, 3, 4, 5] (μλ³Έ μ μ§) // μ€μ μμ: λ°±νΈλνΉ function backtrack(nums, visited, path) { if (path.length === nums.length) { return [path] } const results = [] for (let i = 0; i < nums.length; i++) { if (!visited[i]) { const newVisited = visited.with(i, true) const newPath = [...path, nums[i]] results.push(...backtrack(nums, newVisited, newPath)) } } return results } // μ€μ μμ: μν μ λ°μ΄νΈ function updateState(state, index, value) { return state.with(index, value) } // μ€μ μμ: λΆλ³μ± μ μ§νλ©° μμ function markVisited(grid, row, col) { return grid.with(row, grid[row].with(col, true)) }- μ£Όμμ :
- μμ μΈλ±μ€λ μ§μνμ§ μμ΅λλ€ (
arr.with(-1, 0)β). - λ²μλ₯Ό λ²μ΄λ μΈλ±μ€λ μλ¬λ₯Ό λ°μμν΅λλ€.
- λ¨μΌ μΈλ±μ€λ§ μμ κ°λ₯νλ―λ‘, μ¬λ¬ μΈλ±μ€λ₯Ό μμ νλ €λ©΄ 체μ΄λμ΄ νμν©λλ€.
- μμ μΈλ±μ€λ μ§μνμ§ μμ΅λλ€ (
- κ°μ μ : λΆλ³μ±μ μ μ§νλ©΄μ νΉμ μΈλ±μ€λ§ μμ νλ κ²μ΄ κ°κ²°ν΄μ§λλ€.
findLast(),findLastIndex()- μ€λͺ : λ°°μ΄μ λμμλΆν° μμλ₯Ό νμν©λλ€.
// β κΈ°μ‘΄ λ°©μ const arr = [1, 2, 3, 4, 3, 5] let lastIndex = -1 for (let i = arr.length - 1; i >= 0; i--) { if (arr[i] === 3) { lastIndex = i break } } // β findLast(), findLastIndex() μ¬μ© const arr = [1, 2, 3, 4, 3, 5] const lastThree = arr.findLast((x) => x === 3) // 3 const lastIndex = arr.findLastIndex((x) => x === 3) // 4 // μ€μ μμ: λ§μ§λ§ 쑰건 λ§μ‘± μμ μ°ΎκΈ° function findLastEven(nums) { return nums.findLast((n) => n % 2 === 0) } // μ€μ μμ: ν¨ν΄ λ§€μΉ function findLastMatch(str, pattern) { const chars = str.split('') const index = chars.findLastIndex((c) => pattern.test(c)) return index !== -1 ? chars[index] : null } // μ€μ μμ: μ€νμμ νΉμ κ° μ°ΎκΈ° class Stack { findLast(predicate) { return this.items.findLast(predicate) } }- μ£Όμμ :
- 쑰건μ λ§μ‘±νλ μμκ° μμΌλ©΄
findLast()λundefined,findLastIndex()λ-1μ λ°νν©λλ€. find()μ λμΌν μ½λ°± ν¨μ μκ·Έλμ²λ₯Ό μ¬μ©ν©λλ€.
- 쑰건μ λ§μ‘±νλ μμκ° μμΌλ©΄
- κ°μ μ : λ°°μ΄ λμμλΆν° νμμ΄ νμν κ²½μ° μ½λκ° ν¨μ¬ κ°κ²°ν΄μ§λλ€.
toSpliced()- μ€λͺ : μλ³Έμ λ°κΎΈμ§ μμΌλ©΄μ μμλ₯Ό μμ νκ±°λ μΆκ°ν μ λ°°μ΄μ λ°νν©λλ€.
- μκ³ λ¦¬μ¦ νμ©:
splice()μ λΆλ³μ± λ²μ μΌλ‘, λ°°μ΄μ νΉμ λΆλΆμ μμ νλ©΄μ μλ³Έμ 보쑴ν λ μ¬μ©ν©λλ€.
// β κΈ°μ‘΄ λ°©μ (μλ³Έ λ³κ²½) const arr = [1, 2, 3, 4, 5] const newArr = [...arr] newArr.splice(2, 1, 999) // μλ³Έ λ³΅μ¬ ν μμ // β toSpliced() μ¬μ© const arr = [1, 2, 3, 4, 5] const newArr = arr.toSpliced(2, 1, 999) // [1, 2, 999, 4, 5] console.log(arr) // [1, 2, 3, 4, 5] (μλ³Έ μ μ§) // μ€μ μμ: μμ μμ function removeElement(arr, index) { return arr.toSpliced(index, 1) } // μ€μ μμ: μμ μΆκ° function insertElement(arr, index, value) { return arr.toSpliced(index, 0, value) } // μ€μ μμ: μμ κ΅μ²΄ function replaceElement(arr, index, value) { return arr.toSpliced(index, 1, value) } // μ€μ μμ: μ¬λ¬ μμ κ΅μ²΄ function replaceRange(arr, start, deleteCount, ...items) { return arr.toSpliced(start, deleteCount, ...items) }- μ£Όμμ :
splice(start, deleteCount, ...items)μ λμΌν μκ·Έλμ²λ₯Ό μ¬μ©ν©λλ€.- μμ μΈλ±μ€λ μ§μνμ§ μμ΅λλ€.
- μλ³Έ λ°°μ΄μ μ λ λ³κ²½λμ§ μμ΅λλ€.
- κ°μ μ : λΆλ³μ±μ μ μ§νλ©΄μ λ°°μ΄μ μμ ν μ μμ΄ λ²κ·Έλ₯Ό λ°©μ§ν©λλ€.
6. ES2024 (ES15): λ°μ΄ν° κ·Έλ£Ήν λ° μ§ν© μ°μ°
Node.js μ§μ: Node.js 22.0.0+ (2024λ 4μ)
Object.groupBy(iterable, callback)- μ€λͺ : λ°°μ΄μ μμλ₯Ό νΉμ κΈ°μ€μ λ°λΌ κ°μ²΄λ‘ κ·Έλ£Ήνν©λλ€.
- μκ³ λ¦¬μ¦ νμ©: ν΄μ(Hash) μΉ΄ν
κ³ λ¦¬ λΆλ₯ λ¬Έμ λ₯Ό
reduceμμ΄ ν μ€λ‘ ν΄κ²°ν©λλ€.
// β κΈ°μ‘΄ λ°©μ const people = [ { name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Charlie', age: 25 }, ] const grouped = people.reduce((acc, person) => { const key = person.age if (!acc[key]) acc[key] = [] acc[key].push(person) return acc }, {}) // β Object.groupBy() μ¬μ© const people = [ { name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Charlie', age: 25 }, ] const grouped = Object.groupBy(people, (person) => person.age) // { 25: [{name: 'Alice', age: 25}, {name: 'Charlie', age: 25}], 30: [{name: 'Bob', age: 30}] } // μ€μ μμ: μΉ΄ν κ³ λ¦¬λ³ κ·Έλ£Ήν const products = [ { name: 'Laptop', category: 'Electronics' }, { name: 'Book', category: 'Education' }, { name: 'Phone', category: 'Electronics' }, ] const byCategory = Object.groupBy(products, (p) => p.category) // μ€μ μμ: μ‘°κ±΄λ³ λΆλ₯ const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] const byParity = Object.groupBy(numbers, (n) => (n % 2 === 0 ? 'even' : 'odd')) // { even: [2, 4, 6, 8, 10], odd: [1, 3, 5, 7, 9] } // μ€μ μμ: λ¬Έμμ΄ κΈΈμ΄λ³ κ·Έλ£Ήν const words = ['apple', 'banana', 'cat', 'dog', 'elephant'] const byLength = Object.groupBy(words, (w) => w.length)- μ£Όμμ :
- μ½λ°± ν¨μκ° λ°ννλ ν€λ λ¬Έμμ΄λ‘ λ³νλ©λλ€.
nullμ΄λundefinedλ₯Ό ν€λ‘ λ°ννλ©΄ μλ¬κ° λ°μν©λλ€.- κ²°κ³Ό κ°μ²΄λ λ°°μ΄μ΄λ―λ‘ μμκ° λ³΄μ₯λ©λλ€.
- κ°μ μ :
reduceλ₯Ό μ¬μ©ν 볡μ‘ν κ·Έλ£Ήν λ‘μ§μ ν μ€λ‘ κ°κ²°νκ² ννν μ μμ΅λλ€.
Set.prototypeμ§ν© μ°μ°- μ€λͺ
:
intersection(),union(),difference()λ©μλκ° μΆκ°λμμ΅λλ€. - μκ³ λ¦¬μ¦ νμ©: λ μ§ν© κ°μ κ³΅ν΅ μμ μ°ΎκΈ°λ μ°¨μ§ν© κ³μ° μ μ§μ λ°λ³΅λ¬Έμ λ릴 νμκ° μμ΅λλ€.
// β κΈ°μ‘΄ λ°©μ function intersection(setA, setB) { const result = new Set() for (const item of setA) { if (setB.has(item)) { result.add(item) } } return result } function union(setA, setB) { return new Set([...setA, ...setB]) } function difference(setA, setB) { const result = new Set() for (const item of setA) { if (!setB.has(item)) { result.add(item) } } return result } // β Set λ©μλ μ¬μ© const setA = new Set([1, 2, 3, 4]) const setB = new Set([3, 4, 5, 6]) const intersection = setA.intersection(setB) // Set {3, 4} const union = setA.union(setB) // Set {1, 2, 3, 4, 5, 6} const difference = setA.difference(setB) // Set {1, 2} const symmetricDiff = setA.symmetricDifference(setB) // Set {1, 2, 5, 6} // μ€μ μμ: κ³΅ν΅ μμ μ°ΎκΈ° function findCommonElements(arr1, arr2) { const set1 = new Set(arr1) const set2 = new Set(arr2) return Array.from(set1.intersection(set2)) } // μ€μ μμ: ν©μ§ν© κ³μ° (μ€λ³΅ μ κ±°) function mergeArrays(arr1, arr2) { return Array.from(new Set(arr1).union(new Set(arr2))) } // μ€μ μμ: μ€λ³΅ μ κ±° ν©μ§ν© (κ°λ¨ν λ°©λ²) function uniqueUnion(arr1, arr2) { return [...new Set([...arr1, ...arr2])] } // μ€μ μμ: μ°¨μ§ν© κ³μ° function removeElements(base, toRemove) { return Array.from(new Set(base).difference(new Set(toRemove))) } // μ€μ μμ: λμΉ μ°¨μ§ν© (νμͺ½μλ§ μλ μμ) function findUniqueElements(arr1, arr2) { const set1 = new Set(arr1) const set2 = new Set(arr2) return Array.from(set1.symmetricDifference(set2)) }- μ£Όμμ :
- λ©μλλ μλ‘μ΄ Setμ λ°ννλ©° μλ³Έ Setμ λ³κ²½λμ§ μμ΅λλ€.
intersection(),union(),difference()λ λ€λ₯Έ Setμ μΈμλ‘ λ°μ΅λλ€.symmetricDifference()λ μμͺ½ μ§ν©μ κ°κ°λ§ μλ μμλ₯Ό λ°νν©λλ€.
- κ°μ μ : μ§ν© μ°μ°μ΄ λ΄μ₯ λ©μλλ‘ μ 곡λμ΄ μ½λκ° κ°κ²°νκ³ μ±λ₯λ μ΅μ νλ©λλ€.
- μ€λͺ
:
π‘ μ€μ μ½λ© ν μ€νΈ νκ²½ 체ν¬λ¦¬μ€νΈ
ECMAScript λ²μ λ³ Node.js μ§μ νν©
| ECMAScript λ²μ | μ«μ νκΈ° | Node.js μ΅μ λ²μ | μ£Όμ κΈ°λ₯ |
|---|---|---|---|
| ES2020 | ES11 | Node.js 14.0.0+ | Optional Chaining (?.), Nullish Coalescing (??), BigInt |
| ES2021 | ES12 | Node.js 15.0.0+ | replaceAll(), Numeric Separators (_) |
| ES2022 | ES13 | Node.js 16.9.0+ | Array.at(), Object.hasOwn(), structuredClone() |
| ES2023 | ES14 | Node.js 20.0.0+ | toSorted(), toReversed(), with(), toSpliced(), findLast() |
| ES2024 | ES15 | Node.js 22.0.0+ | Object.groupBy(), Set μ§ν© μ°μ° |
π μ½λ© ν μ€νΈ νλ«νΌλ³ Node.js μ§μ νν© (2024-25 μ λ°μ΄νΈ)
νλ«νΌλ³λ‘ Node.js λ²μ κ³Ό μ§μνλ ECMAScript λ²μ μ΄ λ€λ¦ λλ€. ES2023 λ¬Έλ²μ μ§μνμ§ μλ νλ«νΌλ μ‘΄μ¬νλ―λ‘, μμ ν λ²μ©μ±μ μν΄ ES2022κΉμ§μ λ¬Έλ²μ μ¬μ©νλ κ²μ κΆμ₯ν©λλ€.
- LeetCode: Node.js 20.x (νκ²½μ λ°λΌ ES2023 μ§μ μ¬λΆ μμ΄)
- Programmers: Node.js λ²μ νμΈ νμ (ES2022κΉμ§ μμ )
- λ°±μ€ (BOJ): Node.js 20.x (v20.12.2, ES2023 μ§μ μ¬λΆ νμΈ νμ)
- HackerRank: Node.js 18.x ~ 20.x (νκ²½μ λ°λΌ μμ΄, ES2022κΉμ§ μμ )
- κΈ°ν νλ«νΌ: λλΆλΆ Node.js 18.x μ΄μμ΄μ§λ§, ES2023 λ―Έμ§μ νκ²½ μ‘΄μ¬
β οΈ μ€μ: νλ«νΌλ§λ€ Node.js λ²μ κ³Ό ECMAScript μ§μ μμ€μ΄ λ€λ₯΄λ―λ‘, ES2023 λ¬Έλ²(toSorted(), toReversed(), with() λ±)μ μ¬μ© μ λ°λμ νλ«νΌ μ§μ μ¬λΆλ₯Ό νμΈν΄μΌ ν©λλ€.
π‘ μ½λ© ν μ€νΈ μ΅μ ν λ¬Έλ² ν
1. μμ ν λ²μ© λ¬Έλ² μ¬μ© (κΆμ₯) κ°μ₯ μμ ν λ²μ©μ±μ ν보νλ €λ©΄ ES2022κΉμ§μ λ¬Έλ²μ μ¬μ©νλ κ²μ΄ μ’μ΅λλ€:
Optional Chaining(?.),Nullish Coalescing(??) - ES2020BigInt,replaceAll()- ES2020Array.at(),Object.hasOwn(),structuredClone()- ES2022
2. ES2023 λ¬Έλ² μ¬μ© μ μ£Όμμ¬ν toSorted(), toReversed(), with(), toSpliced(), findLast() λ± ES2023 λ¬Έλ²μ νλ«νΌμ λ°λΌ μ§μλμ§ μμ μ μμ΅λλ€. μ¬μ© μ λ°λμ νλ«νΌμ Node.js λ²μ κ³Ό μ§μ μ¬λΆλ₯Ό νμΈνκ±°λ, λ체 λ°©λ²μ μ€λΉν΄μΌ ν©λλ€.
3. 보μμ μ κ·Όμ΄ νμν κ²½μ° κΈ°μ μ체 μ±μ© μ루μ μ΄λ μ λ°μ΄νΈκ° λλ¦° μΌλΆ νλ«νΌμ μ¬μ ν Node.js 18.x (ES2022) νκ²½μΈ κ²½μ°κ° μμ΅λλ€. κ°μ₯ μμ ν λ²μ©μ±μ ν보νλ €λ©΄ ES2022 λ¬Έλ²μ λ§μ§λ Έμ μΌλ‘ μ‘λ κ²μ΄ μ’μ΅λλ€.
3. λ°νμ νκ²½ νμΈλ² νλ«νΌμ μ νν λ²μ μ΄ κΆκΈνλ€λ©΄ μ½λ μλ¨μ μλ λͺ λ Ήμ΄λ₯Ό μ€ννμ¬ μ§μ νμΈν μ μμ΅λλ€.
console.log(process.version)