λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Language/Javascript

[Javascript]μžλ°”μŠ€ν¬λ¦½νŠΈ μ™„λ²½ κ°€μ΄λ“œ: λͺ¨λ“  λ‚΄μž₯ ν•¨μˆ˜ 총정리!

by YJ Dev 2024. 7. 16.
728x90
λ°˜μ‘ν˜•
SMALL

μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” μ›Ή κ°œλ°œμ—μ„œ κ°€μž₯ 많이 μ‚¬μš©λ˜λŠ” μ–Έμ–΄ 쀑 ν•˜λ‚˜μž…λ‹ˆλ‹€. κ·Έμ€‘μ—μ„œλ„ λ‚΄μž₯ ν•¨μˆ˜λŠ” κ°œλ°œμžλ“€μ΄ μ½”λ“œλ₯Ό 더 효율적으둜 μž‘μ„±ν•  수 있게 λ„μ™€μ€λ‹ˆλ‹€. 이 κΈ€μ—μ„œλŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ λͺ¨λ“  λ‚΄μž₯ ν•¨μˆ˜λ₯Ό μ²΄κ³„μ μœΌλ‘œ μ •λ¦¬ν•˜μ—¬ μ†Œκ°œν•˜κ² μŠ΅λ‹ˆλ‹€.

μžλ°”μŠ€ν¬λ¦½νŠΈ λ‚΄μž₯ν•¨μˆ˜


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);
728x90
λ°˜μ‘ν˜•