JavaScript’s dynamic typing is good and bad at the same time. It’s good because you don’t have to indicate the variable’s type. It’s bad because you can never be sure about the variable’s type.

typeof operator determines the 6 types in JavaScript:

typeof 10;        // => 'number'
typeof 'Hello';   // => 'string'
typeof false;     // => 'boolean'
typeof { a: 1 };  // => 'object'
typeof undefined; // => 'undefined'
typeof Symbol();  // => 'symbol'

As well, instanceof checks the constructor of an instance:

class Cat { }
const myCat = new Cat();

myCat instanceof Cat; // => true

But some behavior of typeof and instanceof can be confusing. The edge cases are wiser to know in advance.

This post describes the pitfalls and remedial workarounds of using typeof and instanceof.

#javascript #typeof #instanceof

The pitfalls and remedial workarounds of using typeof and instanceof
2.25 GEEK