천천히 알아보는 코딩공부

javascript 연산자 본문

JavaScript/기초

javascript 연산자

고기고기물고기 2022. 4. 21. 02:29

+ 연산자

var add1 = 1 + 2;
var add2 = 'my' + 'string';
var add3 = 1 + 'string';
var add4 = 'string' + 2;

console.log(add1); // (출력값) 3
console.log(add2); // (출력값) my string
console.log(add3); // (출력값) 1string
console.log(add4); // (출력값) string2

 

== (동등) 연산자와 === (일치) 연산자 차이점

- ==연산자는 비교하려는 피연산자의 타입이 다를 경우에 타입변환을 거친 다음 비교한다

- === 연산자는 피연산자의 타입이 다를 경우에 타입을 변경하지 않고 비교한다.

 

console.log(1 == '1'); (출력값) true
console.log(1 === '1'); (출력값) false

 

 

!! 연산자

- !! 의 역할은 피연산자를 불린값으로 변환하는 것이다.

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

 

Comments