typeof

typeof operator returns the type information as a string.

The typeof operator is followed by its operand.

operand is an expression which represents the object.

Examples :

Numbers

typeof 32 == number
typeof 45.67 == number
typeof(40) == number
typeof Infinity == number
typeof NaN == number // defined Not a Number
typeof Number(1) == number // but never go with this format

String

typeof '' == String
typeof 'hi' == String
typeof '22' == String
typeof (typeof 22) == String
typeof String('hello') == String // but never go with this format

Booleans

typeof true == boolean
typeof false == boolean

Symbols

typeof Symbol() == Symbol
typeof Symbol('hi') == Symbol

Undefined

typeof undefined == undefined
typeof undeclaredVariables == undefined
typeof declaredButNotDefined == undefined

Objects

typeof {"name" : "blog"} == object
//array
typeof [1,2,3] == object

Function

typeof function(){} == function
typeof class A == function
typeod Math.sin == function

null

typeof null == object

using new function

var name = new String('String');
var num = new Number(100);

typeof name == returns object
typeof num == returns object

var func = new Function(0
typeof func == returns Function

Regular Expressions

typeof /s/ ==  function // from chrome 1-12 and ECMAScript 5.1
typeof /s/ == object // from firefox 5+ to ECMAScript 5.1

Temporal errors

if variables are defined as const

const constantVariable = 'hello';
typeof constantVariable // reference error

Exceptions

typeof document.all == undefined

objects are objects but not function 

typeof alert == object

No comments:

Post a Comment

Overview of AngularJS

AngularJS is an open source web application framework. It is now maintained by Google . The most of the code can be eliminated by using th...