JavaScript의 다양한 연산자에 대해서 알아보자-8가지

JavaScript는 다양한 연산자를 제공하여 코드를 작성하고 데이터를 다룰 때 유용하게 활용할 수 있습니다. 이번 포스팅에서는 주요 연산자에 대해 알아보겠습니다.


1. typeof 연산자

typeof 연산자는 피연산자의 데이터 타입을 반환합니다. 예를 들어:

const typeOfValue = typeof 42; // 'number'
const typeOfString = typeof 'Hello'; // 'string'
const typeOfObject = typeof { name: 'John' }; // 'object'


2. instanceof 연산자

instanceof 연산자는 객체가 특정 클래스 또는 생성자의 인스턴스인지 확인하는 데 사용됩니다.

class Person {}
const person = new Person();
const isPerson = person instanceof Person; // true


3. in 연산자

in 연산자는 객체가 특정 속성을 가지고 있는지 확인합니다.

const person = { name: 'John' };
const hasName = 'name' in person; // true


4. delete 연산자

delete 연산자는 객체의 속성을 삭제합니다.

const person = { name: 'John' };
delete person.name; // 속성 'name' 삭제


5. instanceof 연산자

instanceof 연산자는 객체가 특정 클래스의 인스턴스인지 확인합니다.

class Car {}
const car = new Car();
const isCar = car instanceof Car; // true


6. void 연산자

void 연산자는 표현식을 평가하고 결과를 버립니다. 주로 javascript:void(0)와 같은 방식으로 사용됩니다.


7. typeof 연산자

typeof 연산자는 피연산자의 데이터 타입을 반환합니다.

const typeOfString = typeof 'Hello'; // 'string'
const typeOfFunction = typeof function() {}; // 'function'


8. 비트 연산자

JavaScript는 비트 연산자도 제공합니다. 예를 들어, & (비트 AND), | (비트 OR), ^ (비트 XOR), << (왼쪽 시프트), >> (오른쪽 시프트) 등이 있습니다.

const result = 5 & 3; // 비트 AND 연산: 1
const shifted = 8 >> 1; // 오른쪽 시프트: 4

이러한 연산자는 JavaScript의 다양한 기능을 활용할 때 유용하게 사용됩니다. 코드를 작성하거나 읽을 때 이러한 연산자를 이해하고 활용하는 것이 중요합니다.




Leave a Comment