데일리 리포트/Today I Learn
Unary plus (+) => 단항 연산자(+)
까만색 푸들 까미
2022. 4. 4. 12:23
단항 더하기 연산자(+)는 피연산자 앞에 나와 피연산자로 평가되지만 아직 숫자로 변환되지 않은 경우 숫자로 변환하려고 시도합니다.
"" + 1 // 문자열변환을 이렇게 하는것과 비슷하게 +를 사용하여 숫자 변환을 할수 있다
const x = 1;
const y = -1;
console.log(+x);
// expected output: 1
console.log(+y);
// expected output: -1
console.log(+'');
// expected output: 0
console.log(+true);
// expected output: 1
console.log(+false);
// expected output: 0
console.log(+'hello');
// expected output: NaN
console.log(typeof "100");
// expected output: string
console.log(typeof +"100");
// expected output: number
Unary plus (+) - JavaScript | MDN
The unary plus operator (+) precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already.
developer.mozilla.org