Error - num.replace is not a function

이윤우·2022년 7월 26일
0

JavaScript

목록 보기
3/34

replace() 함수

아래의 코드는 숫자 자료형에 replace함수를 적용하기 때문에 에러가 발생합니다.

const num = 010-1234-1234;

const result = num.replace(/-/g, '');

replace 함수는 문자열에 대해서 적용이 가능한 함수이기 때문에 toString()함수를 이용해서 먼저 문자열로 바꿔줘야 합니다.

const num = 010-1234-1234;

const result = num.toString.replace(/-/g, '');

0개의 댓글