Explain typeof operator in Javascript with example.

javascript

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)

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

testVar = undefined;
var typeVar = typeof testVar;
 
//output: "undefined".
console.log(typeVar)

Related Article: Javascript Data Types Explained

Share and support us

Share on social media and help us reach more people