typeof operator in JavaScript

The typeof operator in JavaScript allows you to probe the data type of its operand,
such as whether a variable is string, numeric, undefined ,object, function etc.
typeof is unary operator the operand will follows the operator.
typeof operator in javascript is complicated, it can be used for many things.
I’ll list use cases of typeof operator in JavaScript.

Simple Example of typeof operator in JavaScript:

var value=5;
alert(typeof value) //alert "number"

var value = "string value";
alert(typeof value) //alert "string";

Why it work if i call like function?

The typeof operator is not a function but You can surround the operand with parentheses so that the expression looks like a function call.
its called `Function-like: typeof` here the parentheses will simply act as a grouping operator you can use it like:-

var value=5;
alert(typeof(value));//alert "number"
var value = "string value";
alert(typeof(value)) //alert "string";
alert(typeof (1 + 1)) //"number"

 

if you pass oprand with comma it will return only last variable’s data type:-

 alert(typeof("a",1,null)) //"object" 

Return value table for typeof operators:

Type of val                           ||                Result

Undefined                                                 ‘undefined’
Null                                                            ‘object’
Boolean                                                     ‘boolean’
Number                                                     ‘number’
String                                                         ‘string’
Object(native and not callable)               ‘object’
function                                                     ‘function’
Number                                                      ‘number’
Number                                                      ‘number’

The typeof operator will return type ‘object’ for null.
The typeof works sufficiently well with primitive values (except null). But it says nothing about object types. The typeof can’t distinguish between objects.

For example, Object, Array and Date objects are same:

alert( typeof {} ) // 'object'
alert( typeof [] ) // 'object'
alert( typeof new Date()) // 'object'

We can also see the difference between a primitive string and new String:

alert( typeof "value" ) // string
alert( typeof new String("value") ) // object

 

Use of “typeof” operator in JavaScript

“typeof” operator can be use for check many things in JavaScript some are listed below:-

1) Checking whether a variable exists and has a value:

“typeof ” operator can be used to check if variable exists and has value. It will return undefined if
variable not declared yet or variable declared but value not assigned to variable. For example:-

alert(typeof undeclaredvariable); //undefined;
//or
var declaredVariable;
alert(typeof declaredVariable); //undefined

2)Checking if value is function:
“typeof ” oprator can be used to check if value is function:-

var value= function(){
retrun "something";
}

alert(typeof value);

 

One Comment

  1. Mary April 3, 2017 Reply

Add a Comment

Your email address will not be published. Required fields are marked *