How to check type of a variable in JavaScript?

To check type of a variable in JavaScript, use the typeof operator.

Here's how you do it:

const aString = 'Hello World!';
const aNumber = 42;
const aBoolean = true;
const anObject = {};
const anArray = [];
const aFunction = function() {};
const anUndefined = undefined;
const aNull = null;
const aNaN = NaN;

console.log(typeof aString); // string
console.log(typeof aNumber); // number
console.log(typeof aBoolean); // boolean
console.log(typeof anObject); // object
console.log(typeof anArray); // object
console.log(typeof aFunction); // function
console.log(typeof anUndefined); // undefined
console.log(typeof aNull); // object
console.log(typeof aNaN); // number