본문 바로가기
Language/JavaScript

[JS] Uncaught TypeError: .replace is not a function

by 그랴 2022. 10. 24.

내 코드

  const filterSource = list.filter((src) => {
    return src.replace(" ", "").includes(search);
  });

 

오류 내용

The "replace is not a function" error occurs when we call the replace() method on a value that is not of type string. To solve the error, convert the value to a string using the toString() method before calling the replace() method.

replace() 를 string이 아닌 값에 쓰면 발생하는 오류였다. 

 

https://bobbyhadz.com/blog/javascript-replace-is-not-a-function

 

TypeError: replace is not a function in JavaScript | bobbyhadz

The "replace is not a function" error occurs when we call the `replace()` method on a value that is not of type `string`. To solve the error, convert the value to a string using the `toString()` method before calling the `replace()` method.

bobbyhadz.com

 

문제 해결

replace()를 사용할 값을 toString() 메소드를 사용하여 문자열로 바꿔주면 된다.

  const filterSource = list.filter((src) => {
    src = src.toString();
    return src.replace(" ", "").includes(search);
  });