[Javascript] 데이터 타입 확인하기 - typeof

JavaScript의 typeof 연산자는 변수 또는 표현식의 데이터 유형을 확인하는 데 사용됩니다.

 

typeof 구문

typeof 연산자는 operand의 타입을 나타내는 문자열을 리턴해줍니다.

typeof operand
typeof(operand)


typeof 예제

typeof 연산 결과 해당 변수의 데이터 유형이 문자열로 나타납니다.

const name = "Bob";
console.log(typeof name); //"string"

const age = 21;
console.log(typeof age); //"number"

const arr = [1, 2, 4];
console.log(typeof arr); //"object"

const obj = {name:'Bob'};
console.log(typeof obj); //"object"

const hello = function(){}
console.log(typeof hello); //"function"

string, number, object, function 외 undefined, boolean, bigint 등도 typeof 연산 결과로 확인할 수 있습니다.

typeof 연산자는 식과 함께 사용할 수도 있습니다.

console.log(typeof (10 + 5)); // “number"
console.log(typeof ("Hello" + "World")); // ”string"
console.log(typeof (10 > 5)); // "boolean"

위의 코드에서 산술, 연결된 문자열, 비교 식에 typeof 연산자를 사용하여 해당 데이터 유형을 확인할 수 있습니다.