728x90

✨white space를 제거하여, 정확한 string값을 제어하기 위해 꼭 필요한 helper method이다.

(e.g., '   Hello World   ') 왼쪽의 string이 있을 때, trim을 사용해주면 문자열 기준 '앞', '뒤'의 white space를 없애준다.
(white space는 characters(space, tab, no-break space, etc.) 와 terminator characters (LF, CR, etc.)이 해당된다)

 

💻Example Code

let str = '   Hello World   ';

console.log(str.trim()); // 'Hello World';

실행 결과(위와 아래의 공백이 다른 것을 볼 수 있다)

위는 console.log(str.trim()); 의 실행결과이며, 아래는 console.log(str);의 실행결과이다.

😋 trim() helper function은 백준에서 algorithm을 풀 때, 받아오는 입력값을 정리할 때 유용하게 사용하고 있다.

👉자세한 내용은 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim

'JavaScript > Built-in Method etc.' 카테고리의 다른 글

Array.prototype.slice()  (0) 2019.12.23
Math.floor()  (0) 2019.12.23
Number.prototype.toFixed()  (0) 2019.12.22
Function.prototype.apply()  (0) 2019.12.22
String.prototype.split()  (0) 2019.12.22

+ Recent posts