[JavaScript] 연산자

suyeon·2022년 6월 7일
0

Frontend

목록 보기
13/19
post-thumbnail

1. + 연산자

  • 문자열 연결 연산
  • 더하기 연산
  • 두 연산자가 모두 숫자일 경우에만 더하기 연산이 수행되고, 나머지는 문자열 연결 연산이 이뤄진다.
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

2. typeof 연산자

  • 피연산자의 타입을 문자열 형태로 리턴한다.
  • typeof(자료)

3. ==(동등) 연산자와 ===(일치) 연산자

== : 자료형 비교(x), 값만 비교(o)
=== : 자료형 비교(o) + 값 비교(o)

4. !! 연산자

  • 피연산자를 불린값으로 변환
console.log(!!0); // false
console.log(!!1); // true
console.log(!!'string'); // true
console.log(!!'');
console.log(!!true);
console.log(!!false);
console.log(!!null);
console.log(!!undefined); // false
console.log(!!{}); // true
console.log(!![1,2,3]); // true
  • 객체는 값이 비어있는 빈 객체라도 true로 변환된다.

0개의 댓글