JS 기본객체별 내장메서드 정리

Number

0.1+0.2 ===0.3 // retrun false

// 소수점까지 일치하는 Number 처리
function isEqual(a,b){
    return Math.abs(a-b) < Number.EPSIOLON //(가장작은 단위)
}

isEqaul(0.1+0.2 , 0.3) // return true

Number.MAX_SAFE_INTEGER
Number.MIN_SAFE_INTEGER

Number.isInteger(12)
Number.isNaN(NaN)  // NaN은 서로 같지 않으므로 비교할 때 반드시 isNaN을 사용

Number.toFixed()  // 소수점 아래를 반올림
Number.toPrecision() // 정확도로 몇자리까지 제대로 쓸건지 확인
(23).toString(_) // 문자열로 만드는데 _에따라 진법이 달라짐


Math

Math.PI
Math.abs
Math.round
Math.ceil
Math.floor
Math.sqrt
Math.random
Math.pow
Math.max
Math.min

Date

new Date(0) // 1970 1월 1일 00:00:00 기준

new Date('2025/01/31/10:00:00')  //  soqnsms Date.parse로 변환 가능한 format

Date.now()

Date.getFullYear()

Date.setFullYear()

Date.getMonth()
Date.setMonth()

Date.getDate()
Date.setDate()

// 요일 0~6
Date.getDay();

today.toString();
today.toISOString();
today.toLocaleString()'ko-KR';

String

str.length
new String('test');

str.indexOf('or') // 두글자도 가능 return index or -1
str.inclueds('or',startIdx) // return false
str.search(/of/) // 정규식 검사도 가능

str.startsWith
str.endsWith
str.charAt(idx) // 해당 문자열 반환 넘을경우 빈문자열 ''

str.substring(startIdx,endIdx) // 음의 인덱스 사용 불가, 음의 인덱스는 slice사용

str.toUpperCase
str.toLowerCase
str.trim      // 파생으로 앞 or 뒤만 필요하면 trimStart, trimEnd

str.replace(/hello/, '')   // 제거용으로 많이 쓰임
'Hello world'.replace('world','<strong>$&</sctong>')  // $&은 찾은 내용 대타


str.repeat(2) // return str+str
str.split(//, _) // 

Set

const set = new Set([1,2,23,34]]);

set.size
set.add(25)   // return set   chainning 가능
set.has(25) // return true
set.delete(25) // 
set.clear()

set.intersection(anotherSet)  // 교집합
set.union(anotherSet) // 합집합
set.difference(anotherSet)  // 차집합

new Set([...set].filter((item)=>!anoterSet.has(item))) // anotherSet이 안가진 데이터만

set.isSuperset(anotherSet)

Map

new Map();

new Map([['key1','value1'],['key2','value2']])

map.size // 2
map.set('key3','value3')
map.has("key2")  // true
map.get("key2")  // value2


map.keys() => ['key1','key2',"key3"] // 입력됐던 순서가 유지됨