[JS] JavaScript 내장 함수와 메서드

artp·2025년 9월 19일

javascript

목록 보기
28/50
post-thumbnail

JavaScript 내장 함수와 메서드

1. 내장 함수와 메서드의 차이

  • 내장 함수 (Built-in Function): 특정 객체에 속하지 않고 독립적으로 호출할 수 있는 함수입니다.
    • 예: parseInt(), isNaN()
  • 메서드 (Method): 특정 객체(예: 문자열, 숫자, 배열)에 속해 있어, 해당 객체를 통해서만 호출할 수 있는 함수입니다.
    • 예: string.toUpperCase(), array.push()
// 내장 함수의 예
const numString = "123.45";
const integer = parseInt(numString); // 특정 객체 없이 바로 호출
console.log(integer); // 123

// 메서드의 예
const message = "hello";
const upperMessage = message.toUpperCase(); // 'message'라는 문자열 객체를 통해 호출
console.log(upperMessage); // "HELLO"

2. 주요 문자열 메서드 (String Methods)

문자열(String) 객체는 텍스트 데이터를 조작하는 데 유용한 다양한 내장 메서드를 제공합니다.

string.charAt(index)

지정된 인덱스(위치)에 있는 문자를 반환합니다.

let str = "JavaScript";
console.log(str.charAt(0)); // "J"
console.log(str.charAt(4)); // "S"

string.includes(searchValue)

문자열이 특정 문자열(searchValue)을 포함하고 있는지 확인하여 true 또는 false를 반환합니다. (대소문자 구분)

let message = "Hello World";
console.log(message.includes("World")); // true
console.log(message.includes("world")); // false

string.split(separator)

문자열을 지정된 구분자(separator)를 기준으로 나누어 배열로 반환합니다.

let text = "apple,banana,orange";
let fruits = text.split(",");
console.log(fruits); // ["apple", "banana", "orange"]

array.join(separator)
split()과 반대 역할을 하는 join()은 배열(Array)의 메서드입니다. 배열의 모든 원소를 연결해 하나의 문자열로 만듭니다.

let fruits = ["apple", "banana", "orange"];
let text = fruits.join(", ");
console.log(text); // "apple, banana, orange"

string.slice(startIndex, endIndex)

문자열의 일부를 추출하여 새로운 문자열을 반환합니다. 원본 문자열은 변경되지 않습니다.

  • endIndex는 포함되지 않습니다.
  • endIndex를 생략하면 문자열 끝까지 추출합니다.
let str = "JavaScript";
console.log(str.slice(0, 4)); // "Java"
console.log(str.slice(4));    // "Script"

string.replace(searchValue, newValue)

문자열에서 특정 부분(searchValue)을 찾아 다른 문자열(newValue)로 교체합니다. 기본적으로 첫 번째로 발견된 부분만 교체합니다.

let message = "Hello World, World!";
let newMessage = message.replace("World", "JavaScript");
console.log(newMessage); // "Hello JavaScript, World!"

string.toLowerCase()string.toUpperCase()

문자열의 모든 문자를 각각 소문자 또는 대문자로 변환하여 새로운 문자열을 반환합니다.

let text = "Hello World";
console.log(text.toLowerCase()); // "hello world"
console.log(text.toUpperCase()); // "HELLO WORLD"

string.trim()

문자열의 양쪽 끝에 있는 공백(whitespace)을 제거합니다.

let text = "  Hello World  ";
console.log(text.trim()); // "Hello World"

3. 주요 숫자 관련 기능

parseInt(string) (함수)

문자열을 정수(integer)로 변환합니다. 숫자가 아닌 문자를 만나면 그 이전까지의 숫자만 변환하며, 숫자로 시작하지 않으면 NaN(Not-a-Number)을 반환합니다.

console.log(parseInt("123.45")); // 123
console.log(parseInt("123abc")); // 123
console.log(parseInt("abc123")); // NaN

number.toFixed(n) (메서드)

숫자를 지정된 소수점 n자리까지 반올림하여 문자열로 반환합니다.

let num = 3.14159;
console.log(num.toFixed(2)); // "3.14"
console.log(num.toFixed(4)); // "3.1416"

4. Math 객체

Math 객체는 수학적인 상수와 함수를 위한 속성과 메서드를 가진 내장 객체입니다.

Math.random()

0 이상 1 미만의 난수(랜덤 소수)를 반환합니다. 이를 활용하여 특정 범위의 정수를 만들 수 있습니다.

// 0 이상 1 미만의 난수
console.log(Math.random());

// 1부터 10까지의 정수 중 랜덤한 수
let randomInt = Math.floor(Math.random() * 10) + 1;
console.log(randomInt);
  • Math.floor(): 주어진 숫자보다 작거나 같은 가장 큰 정수를 반환합니다. (내림)

5. 주요 내장 객체 (Key Built-in Objects)

JavaScript에는 위에서 다룬 기능 외에도 다양한 내장 객체들이 있으며, 각각 고유한 속성과 메서드를 가집니다.

Array

배열을 다루기 위한 객체. 데이터를 모아두고 순회하거나 가공할 때 사용됩니다.

  • push(): 배열 끝에 요소 추가
  • pop(): 배열 끝 요소 제거
  • slice(start, end): 배열의 일부를 잘라서 새로운 배열 반환
  • forEach(): 배열 요소를 순회하며 콜백 실행
const arr = [1, 2, 3];
arr.push(4);          // [1, 2, 3, 4]
arr.pop();            // [1, 2, 3]
const sub = arr.slice(0, 2); // [1, 2]
arr.forEach(v => console.log(v)); // 1, 2, 3

Date

날짜와 시간을 다루기 위한 객체.

  • new Date(): 현재 날짜와 시간 생성
  • getFullYear(): 연도 반환
  • getMonth(): 월 반환 (0부터 시작 → 0 = 1월)
  • toLocaleString(): 지역에 맞는 형식으로 날짜와 시간 출력
const now = new Date();
console.log(now.getFullYear());   // 2025
console.log(now.getMonth() + 1);  // 9 (9월)
console.log(now.toLocaleString()); // 예: "2025. 9. 19. 오전 9:15:30"

Math

수학적 연산을 위한 객체.

  • Math.random(): 0 이상 1미만 난수 반환
  • Math.floor(x): 소수점 버림
  • Math.ceil(x): 소수점 올림
  • Math.max(...nums): 최댓값 반환
console.log(Math.random());     // 0.12345...
console.log(Math.floor(3.7));   // 3
console.log(Math.ceil(3.1));    // 4
console.log(Math.max(1, 5, 9)); // 9

Object

객체를 다루기 위한 일반적인 기능 제공.

  • Object.keys(obj): 객체의 키 배열 반환
  • Object.values(ojb): 객체의 값 배열로 반환
  • Object.entries(obj): 키-값 쌍을 배열로 반환
const user = { id: 1, name: "철수" };
console.log(Object.keys(user));   // ["id", "name"]
console.log(Object.values(user)); // [1, "철수"]
console.log(Object.entries(user)); // [["id", 1], ["name", "철수"]]

String

문자열을 다루기 위한 객체.

  • length: 문자열 길이
  • toUpperCase(): 대문자로 변환
  • toLowerCase(): 소문자로 변환
  • includes(sub): 부분 문자열 포함 여부 확인
const str = "Hello World";
console.log(str.length);          // 11
console.log(str.toUpperCase());   // "HELLO WORLD"
console.log(str.toLowerCase());   // "hello world"
console.log(str.includes("World")); // true

Number

숫자와 관련된 기능을 제공.

  • Number.isInteger(x): 정수 여부 확인
  • toFixed(n): 소수점 n자리까지 고정
  • parseInt(str): 문자열을 정수로 변환
  • parseFloat(str): 문자열을 부동소수점으로 변환
console.log(Number.isInteger(10));    // true
console.log((3.14159).toFixed(2));    // "3.14"
console.log(parseInt("123px"));       // 123
console.log(parseFloat("3.14원"));    // 3.14
profile
donggyun_ee

0개의 댓글