본문 바로가기
Script/JavaScript

[JavaScript] Chapter 04. 연산자

by song.ift 2022. 12. 8.

typeof 연산자

  • 변수, 객체, 함수 또는 표현식의 데이터 타입을 반환해주는 연산자
typeof "John"                   // Return string
typeof 3.14                     // Return number
typeof Nan                      // Return number
typeof false                    // Return boolean
typeof [1, 2, 3, 4]             // Return object
typeof {name:'John', age:34}    // Return object
typeof new Data()               // Return object
typeof function(){}             // Return function
typeof myCar                    // Return undefined
typeof null                     // Return object

 

instanceof  연산자

  • 어떤 객체에 대하여 지정한 객체의 데이터 타입에 대한 결과를 반환하는 연산자
  • 생성자의 prototype 속성이 객체의 프로토타입 체인 어딘가 존재하는지 판별한다
  • 결과 값은 참(true) 또는 거짓(false)

[표현식]

변수 instanceof Array

function Car(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
}

const auto = new Car("Honda", "Accord", 1998);

console.log(auto instanceof Car);               // true

console.log(auto instanceof Object);            // true
console.log(Car.prototype instanceof Object);   // true

 

in 연산자

  • 어떤 객체안에 어떤 값이 존재하는지 결과값을 반환하는 연산자
  • 결과 값은 참(true) 또는 거짓(false)

[표현식]

const cars

const cars = ["Saab", "Volvo", "BMW"];

document.write("Saab" in cars);     // false
document.write(0 in cars);          // true
document.write(3 in cars);          // false


const person = [firstName:"John", lastName:"Doe", age:50];

document.write("firstName" in person);    // true
document.write("age" in person);          // true

// in 연산자는
// 해당 값을 찾는 것이 아니라 
// 배열안의 요소, 인덱스, 키값에 대해서 찾았을 때, 결과를 반환한다.

 


 

비트 연산자

  • 32비트 숫자에서 작동
    • 피연산자는 32비트 숫자로 변환
연산자 연산자 설명 예제
& AND a와 b 둘 다 1이면 1
| OR a와 b 둘 중 하나가 1이면 1
~ NOT 비트가 1이면 0, 0이면 1로 변경
^ XOR 두 비트가 서로 다르면 1 반환
<< Left Shift 비트를 왼쪽으로 이동
>> Right Shift 부호 유지하면서, 비트를 오른쪽으로 이동

 

비트 연산자 : a & b

비트 연산자 : a | b

비트 연산자 : ~ a

비트 연산자 : a ^ b

 

댓글