본문 바로가기

Dev-/java script, jQuery, Ajax

자바스크립트 typeof() vs instanceof() 차이

1. typeof

해당 변수의 primitive(기본)타입을 반환해준다.

  • undefined
  • boolean
  • string
  • number
  • object
  • function

2. instanceof
비교연산자로, object타입의 __proto__타입과 비교해 확인해준다.
(primitive타입 변수와 비교할 시 에러)
const TestObj = function () {};
const TestObj2 = function () {};

const testObj = new TestObj();

console.log(typeof testObj); /** 'object' */
console.log(testObj instanceof Object); /** true */
console.log(testObj instanceof TestObj); /** true */
console.log(testObj instanceof TestObj2); /** false */