[연산자] 단항 더하기 (+) 연산자

posinity·2022년 11월 18일
0

JavaScript

목록 보기
8/31

정의

단항 더하기 연산자(+)는 피연산자 앞에 위치하며 피연산자를 평가하지만, 만약 피연산자가 숫자가 아니라면 숫자로 변환을 시도한다.

예시

const x = 1;
const y = -1;

console.log(+x);
// expected output: 1

console.log(+y);
// expected output: -1

console.log(+'');
// expected output: 0
// 빈 문자열이므로 0으로 변환

console.log(+true);
// expected output: 1
// ture는 1로 변환

console.log(+false);
// expected output: 0

console.log(+'hello');
// expected output: NaN

+해서 'hello'가 숫자형으로 바뀌었는데
어찌됐든 숫자가 아니기 때문에
NaN으로 뜸

참고 : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Unary_plus

profile
문제를 해결하고 가치를 제공합니다

0개의 댓글