js

[JavaScript] ์™ผ์ชฝ 0 ์ œ๊ฑฐํ•˜๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•

natrue 2022. 4. 13. 10:09
728x90

1. .replace(/(^0+)/, ""); ์ •๊ทœ์‹ ์‚ฌ์šฉ.

var num = '000010111200';
num.replace(/(^0+)/, ""); 

result : 10111200

2. Number() ํ•จ์ˆ˜ ์‚ฌ์šฉ. ( = ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜)

var num = '000010111200';
Number(num); 

result : 10111200

* JavaScript ์—์„œ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฐ’์— Number()ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•  ๊ฒฝ์šฐ NaN์ด ์ถœ๋ ฅ = NaN(Not-A-Number)