μλ°μ€ν¬λ¦½νΈλ μΉ κ°λ°μμ κ°μ₯ λ§μ΄ μ¬μ©λλ μΈμ΄ μ€ νλμ λλ€. κ·Έμ€μμλ λ΄μ₯ ν¨μλ κ°λ°μλ€μ΄ μ½λλ₯Ό λ ν¨μ¨μ μΌλ‘ μμ±ν μ μκ² λμμ€λλ€. μ΄ κΈμμλ μλ°μ€ν¬λ¦½νΈμ λͺ¨λ λ΄μ₯ ν¨μλ₯Ό 체κ³μ μΌλ‘ μ 리νμ¬ μκ°νκ² μ΅λλ€.
β£ λͺ©μ°¨
- μλ°μ€ν¬λ¦½νΈ λ΄μ₯ ν¨μλ?π€
- λ¬Έμμ΄(String) κ΄λ ¨ ν¨μπ
- λ°°μ΄(Array) κ΄λ ¨ ν¨μπ
- μ«μ(Number) κ΄λ ¨ ν¨μπ’
- κ°μ²΄(Object) κ΄λ ¨ ν¨μπ¨
- λ μ§(Date) κ΄λ ¨ ν¨μπ
- ν¨μ(Function) κ΄λ ¨ ν¨μβ
- μ νΈλ¦¬ν°(Utility) ν¨μπ§
01. μλ°μ€ν¬λ¦½νΈ λ΄μ₯ ν¨μλ?π€
λ΄μ₯ ν¨μμ κ°λ κ³Ό μν
λ΄μ₯ ν¨μλ μλ°μ€ν¬λ¦½νΈμμ κΈ°λ³Έμ μΌλ‘ μ 곡νλ ν¨μλ€λ‘, λ³λμ μ μ μμ΄ λ°λ‘ μ¬μ©ν μ μμ΅λλ€. μ΄λ¬ν ν¨μλ€μ κ°λ°μμ μμ°μ±μ λμ¬μ£Όλ©°, 볡μ‘ν μμ μ μ½κ² μνν μ μκ² ν©λλ€.
λ΄μ₯ ν¨μλ₯Ό μ¬μ©νλ μ΄μ μ μ₯μ
λ΄μ₯ ν¨μλ μ½λμ κ°λ μ±μ λμ΄κ³ , νμ€νλ λ°©λ²μΌλ‘ νΉμ μμ μ μνν μ μκ² ν΄ μ€λλ€. μ΄λ₯Ό ν΅ν΄ κ°λ°μλ μ½λ μμ± μκ°μ μ€μ΄κ³ , λ³΄λ€ μμ μ μΈ μ½λλ₯Ό μμ±ν μ μμ΅λλ€.
02. λ¬Έμμ΄(String) κ΄λ ¨ ν¨μπ
- charAt(): λ¬Έμμ΄μμ νΉμ μμΉμ μλ λ¬Έμλ₯Ό λ°ν
let str = "Hello";
console.log(str.charAt(1)); // "e"
- concat(): λ κ° μ΄μμ λ¬Έμμ΄μ κ²°ν©
let str1 = "Hello";
let str2 = "World";
console.log(str1.concat(" ", str2)); // "Hello World"
- includes(): λ¬Έμμ΄μ νΉμ λ¬Έμμ΄μ΄ ν¬ν¨λμ΄ μλμ§ νμΈ
let str = "Hello World";
console.log(str.includes("World")); // true
- indexOf(): λ¬Έμμ΄μμ νΉμ λ¬Έμμ΄μ 첫 λ²μ§Έ μμΉλ₯Ό λ°ν
let str = "Hello World";
console.log(str.indexOf("World")); // 6
- slice(): λ¬Έμμ΄μ μΌλΆλΆμ λ°ν
let str = "Hello World";
console.log(str.slice(0, 5)); // "Hello"
- split(): λ¬Έμμ΄μ λ°°μ΄λ‘ λΆν
let str = "Hello World";
console.log(str.split(" ")); // ["Hello", "World"]
- toLowerCase(): λ¬Έμμ΄μ μλ¬Έμλ‘ λ³ν
let str = "Hello World";
console.log(str.toLowerCase()); // "hello world"
- toUpperCase(): λ¬Έμμ΄μ λλ¬Έμλ‘ λ³ν
let str = "Hello World";
console.log(str.toUpperCase()); // "HELLO WORLD"
- padStart(): λ¬Έμμ΄μ μμμ λ€λ₯Έ λ¬Έμμ΄λ‘ μ±μ, μ£Όμ΄μ§ κΈΈμ΄λ₯Ό λ§μ‘±νλ μλ‘μ΄ λ¬Έμμ΄μ λ°ν
let str = "5";
console.log(str.padStart(2, "0")); // "05"
- padEnd(): λ¬Έμμ΄μ λμ λ€λ₯Έ λ¬Έμμ΄λ‘ μ±μ, μ£Όμ΄μ§ κΈΈμ΄λ₯Ό λ§μ‘±νλ μλ‘μ΄ λ¬Έμμ΄μ λ°ν
let str = "5";
console.log(str.padEnd(2, "0")); // "50"
- length: λ¬Έμμ΄μ κΈΈμ΄λ₯Ό λ°ν
let str = "Hello";
console.log(str.length); // 5
03. λ°°μ΄(Array) κ΄λ ¨ ν¨μπ
- concat(): λ κ° μ΄μμ λ°°μ΄μ κ²°ν©
let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
console.log(arr1.concat(arr2)); // [1, 2, 3, 4, 5, 6]
- every(): λ°°μ΄μ λͺ¨λ μμκ° μ£Όμ΄μ§ 쑰건μ λ§μ‘±νλμ§ νμΈ
let arr = [1, 2, 3, 4, 5];
console.log(arr.every(num => num > 0)); // true
- filter(): μ£Όμ΄μ§ 쑰건μ λ§μ‘±νλ λͺ¨λ μμλ₯Ό μλ‘μ΄ λ°°μ΄λ‘ λ°ν
let arr = [1, 2, 3, 4, 5];
console.log(arr.filter(num => num > 3)); // [4, 5]
- forEach(): λ°°μ΄μ κ° μμμ λν΄ μ£Όμ΄μ§ ν¨μλ₯Ό μ€ν
let arr = [1, 2, 3];
arr.forEach(num => console.log(num * 2)); // 2, 4, 6
- indexOf(): λ°°μ΄μμ νΉμ μμμ 첫 λ²μ§Έ μμΉλ₯Ό λ°ν
let arr = [1, 2, 3, 4, 5];
console.log(arr.indexOf(3)); // 2
- join(): λ°°μ΄μ λͺ¨λ μμλ₯Ό λ¬Έμμ΄λ‘ κ²°ν©
let arr = ["Hello", "World"];
console.log(arr.join(" ")); // "Hello World"
- map(): λ°°μ΄μ κ° μμμ μ£Όμ΄μ§ ν¨μλ₯Ό μ μ©ν κ²°κ³Όλ₯Ό μλ‘μ΄ λ°°μ΄λ‘ λ°ν
let arr = [1, 2, 3];
console.log(arr.map(num => num * 2)); // [2, 4, 6]
- reduce(): λ°°μ΄μ λͺ¨λ μμλ₯Ό νλμ κ°μΌλ‘ κ²°ν©
let arr = [1, 2, 3, 4, 5];
console.log(arr.reduce((sum, num) => sum + num)); // 15
- sort(): λ°°μ΄μ μμλ₯Ό μ λ ¬
let arr = [5, 3, 8, 1];
console.log(arr.sort((a, b) => a - b)); // [1, 3, 5, 8]
04. μ«μ(Number) κ΄λ ¨ ν¨μπ’
- toFixed(): μμμ μ리μλ₯Ό μ§μ νμ¬ λ¬Έμμ΄λ‘ λ³ν
let num = 5.56789;
console.log(num.toFixed(2)); // "5.57"
- toPrecision(): μ 체 μλ¦Ώμλ₯Ό μ§μ νμ¬ λ¬Έμμ΄λ‘ λ³ν
let num = 5.56789;
console.log(num.toPrecision(3)); // "5.57"
- toString(): μ«μλ₯Ό λ¬Έμμ΄λ‘ λ³ν
let num = 255;
console.log(num.toString(16)); // "ff" (16μ§μ)
- valueOf(): κ°μ²΄μ μμ κ°μ λ°ν
let num = new Number(123);
console.log(num.valueOf()); // 123
- isNaN(): κ°μ΄ NaNμΈμ§ μ¬λΆλ₯Ό νμΈ
console.log(isNaN("Hello")); // true
console.log(isNaN(123)); // false
- isFinite(): κ°μ΄ μ νν μ«μμΈμ§ μ¬λΆλ₯Ό νμΈ
console.log(isFinite(10)); // true
console.log(isFinite(Infinity)); // false
- parseInt(): λ¬Έμμ΄μ μ μλ‘ λ³ν
console.log(parseInt("10")); // 10
console.log(parseInt("10.5")); // 10
- parseFloat(): λ¬Έμμ΄μ λΆλ μμμ μ«μλ‘ λ³ν
console.log(parseFloat("10.5")); // 10.5
console.log(parseFloat("10")); // 10
- Number.isInteger(): κ°μ΄ μ μμΈμ§ μ¬λΆλ₯Ό νμΈ
console.log(Number.isInteger(10)); // true
console.log(Number.isInteger(10.5)); // false
- Number.isSafeInteger(): κ°μ΄ μμ ν μ μμΈμ§ μ¬λΆλ₯Ό νμΈ
console.log(Number.isSafeInteger(10)); // true
console.log(Number.isSafeInteger(Math.pow(2, 53))); // false
- Math.round(): μ«μλ₯Ό λ°μ¬λ¦Ό
let num = 5.56789;
console.log(Math.round(num)); // 6
- Math.floor(): μ«μλ₯Ό λ΄λ¦Ό
let num = 5.56789;
console.log(Math.floor(num)); // 5
- Math.ceil(): μ«μλ₯Ό μ¬λ¦Ό
let num = 5.56789;
console.log(Math.ceil(num)); // 6
- Math.trunc(): μμμ μ λ²λ¦Ό
let num = 5.56789;
console.log(Math.trunc(num)); // 5
- Math.random(): 0 μ΄μ 1 λ―Έλ§μ λμλ₯Ό λ°ν
console.log(Math.random()); // 0.123456789 (μμ)
05. κ°μ²΄(Object) κ΄λ ¨ ν¨μπ¨
- assign(): νλ μ΄μμ μΆμ² κ°μ²΄λ‘λΆν° λμ κ°μ²΄λ‘ μμ±μ 볡μ¬
let target = { a: 1 };
let source = { b: 2 };
let returnedTarget = Object.assign(target, source);
console.log(returnedTarget); // { a: 1, b: 2 }
- create(): μ§μ λ νλ‘ν νμ κ°μ²΄μ μμ±μΌλ‘ μλ‘μ΄ κ°μ²΄λ₯Ό μμ±
let person = {
isHuman: false,
printIntroduction: function() {
console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
}
};
let me = Object.create(person);
me.name = "Matthew";
me.isHuman = true;
me.printIntroduction(); // "My name is Matthew. Am I human? true"
- keys(): κ°μ²΄μ μ΄κ±° κ°λ₯ν μμ± μ΄λ¦μ λ°°μ΄λ‘ λ°ν
let obj = { a: 1, b: 2, c: 3 };
console.log(Object.keys(obj)); // ["a", "b", "c"]
- values(): κ°μ²΄μ μ΄κ±° κ°λ₯ν μμ± κ°μ λ°°μ΄λ‘ λ°ν
let obj = { a: 1, b: 2, c: 3 };
console.log(Object.values(obj)); // [1, 2, 3]
- entries(): κ°μ²΄μ μ΄κ±° κ°λ₯ν μμ± [key, value] μμ λ°°μ΄λ‘ λ°ν
let obj = { a: 1, b: 2, c: 3 };
console.log(Object.entries(obj)); // [["a", 1], ["b", 2], ["c", 3]]
06. λ μ§(Date) κ΄λ ¨ ν¨μπ
- getDate(): λ μ§μ μΌμ λ°ν
let date = new Date();
console.log(date.getDate()); // 15 (μμ)
- getDay(): λ μ§μ μμΌμ λ°ν
let date = new Date();
console.log(date.getDay()); // 3 (μμμΌ, μμ)
- getFullYear(): λ μ§μ μ°λλ₯Ό λ°ν
let date = new Date();
console.log(date.getFullYear()); // 2024 (μμ)
- getMonth(): λ μ§μ μμ λ°ν (0λΆν° μμ)
let date = new Date();
console.log(date.getMonth()); // 6 (7μ, μμ)
- getTime(): 1970λ 1μ 1μΌ 00:00:00 UTC μ΄νμ λ°λ¦¬μ΄λ₯Ό λ°ν
let date = new Date();
console.log(date.getTime()); // 1625678400000 (μμ)
- setDate(): λ μ§μ μΌμ μ€μ
let date = new Date();
date.setDate(1);
console.log(date); // μλ‘μ΄ λ μ§ κ°μ²΄ (μμ)
- setFullYear(): λ μ§μ μ°λλ₯Ό μ€μ
let date = new Date();
date.setFullYear(2025);
console.log(date); // μλ‘μ΄ λ μ§ κ°μ²΄ (μμ)
07. ν¨μ(Function) κ΄λ ¨ ν¨μβ
- apply(): μ£Όμ΄μ§ this κ°κ³Ό λ°°μ΄λ‘ μ 곡λ μΈμλ‘ ν¨μλ₯Ό νΈμΆ
function sum(a, b) {
return a + b;
}
console.log(sum.apply(null, [1, 2])); // 3
- bind(): μλ‘μ΄ ν¨μλ₯Ό μμ±νκ³ μ£Όμ΄μ§ this κ° λ° μΈμλ‘ ν¨μλ₯Ό νΈμΆ
let module = {
x: 42,
getX: function() {
return this.x;
}
};
let unboundGetX = module.getX;
let boundGetX = unboundGetX.bind(module);
console.log(boundGetX()); // 42
- call(): μ£Όμ΄μ§ this κ°κ³Ό μΈμλ‘ ν¨μλ₯Ό νΈμΆ
function greet() {
console.log(`Hello, my name is ${this.name}`);
}
let person = { name: "John" };
greet.call(person); // "Hello, my name is John"
- toString(): ν¨μλ₯Ό λ¬Έμμ΄λ‘ λ³ν
function sum(a, b) {
return a + b;
}
console.log(sum.toString()); // "function sum(a, b) { return a + b; }"
08. μ νΈλ¦¬ν°(Utility) ν¨μπ§
- alert(): κ²½κ³ λν μμλ₯Ό νμ
alert("Hello, world!");
- console.log(): μ½μμ λ©μμ§λ₯Ό μΆλ ₯
console.log("Hello, world!");
- setTimeout(): μΌμ μκ° νμ ν¨μλ₯Ό μ€ν
setTimeout(() => {
console.log("3μ΄ νμ μ€νλ©λλ€.");
}, 3000);
- setInterval(): μΌμ μκ° κ°κ²©μΌλ‘ ν¨μλ₯Ό λ°λ³΅ μ€ν
setInterval(() => {
console.log("1μ΄λ§λ€ μ€νλ©λλ€.");
}, 1000);