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:

1
2
3
4
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.

1
2
3
4
5
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