JavaScript
[JavaScript] 다시 공부하는 JavaScript 4. 연산자
Serina_Heo
2022. 1. 15. 15:09
1. + 연산자
+ 연산자는 더하기 연산과 문자열 연결 연산을 수행한다.
피연산자가 모두 숫자일 경우에만 더하기 연산이 수행되고,
나머지의 경우 문자열 연결 연산이 수행된다.
var addNums = 1 + 2;
var addNumString = 1 + '2';
var addStrings = '1'+'string';
console.log(addNums); // 3 출력됨
console.log(addNumString); // 12 출력됨
console.log(addStrings); // 1string 출력됨
2. typeof 연산자
typeof 연산자는 피연산자의 타입을 문자열 형태로 반환한다.
기본 타입 | 숫자 | 'number' |
기본 타입 | 문자열 | 'string' |
기본 타입 | 불린값 | 'boolean' |
기본 타입 | null | 'object' |
기본 타입 | undefined | 'undefined' |
참조 타입 | 객체 | 'object' |
찹조 타입 | 배열 | 'object' |
참조 타입 | 함수 | 'function' |
3. 동등 연산자 (==)와 일치 연산자(===)
JavaScript에서 ==는 값만 비교하고, ===는 값과 타입을 비교한다.
그렇게 때문에 JavaScript에서는 ===로 비교하는 것을 추천한다.
console.log(1 == '1'); // true 출력됨
console.log(1 === '1'); // false 출력됨
4. !! 연산자
!!의 역할은 피연산자를 불린값으로 변환하는 것이다. 즉, 다른 타입의 데이터를 불린값으로 형변환하기 위해 사용하는 것이다.
빈 객체라도 true라는 점을 기억해야한다.
console.log(!!0); // false
console.log(!!1); // true
console.log(!!'string'); // true
console.log(!!''); // false
console.log(!!true); // true
console.log(!!false); // false
console.log(!!null); // false
console.log(!!undefined); // false
console.log(!!{}); // true
console.log(!![1,2,3]); // true
[출처]
인사이드 자바스크립트 / 송형주, 고현준 지음/ 한빛미디어
** 해당 게시물은 '인사이드 자바스크립트'를 보고 개인적인 용도로 학습하기 위하여 정리, 게시한 글입니다.