DEV Community

Cover image for Explain typeof operator in Javascript with example
Srijan
Srijan

Posted on • Originally published at hackinbits.com

Explain typeof operator in Javascript with example

This post first appeared on hackinbits.com

The typeof operator helps in finding the type of value that your variable is storing. It returns a string indicating the type of argument. It supports two syntaxes:

  1. As an operator: typeof v.
  2. As a function: typeof (v).

Example:

var exp = typeof "Welcome to hackinbits.com";

//output: "string".
console.log(exp)
Enter fullscreen mode Exit fullscreen mode

You can also use typeof operator to check whether a value is undefined or not.

let testVar = undefined;
var typeVar = typeof testVar;

//output: "undefined".
console.log(typeVar)
Enter fullscreen mode Exit fullscreen mode

Related Article

Top comments (0)