Students Empire

Learn Something New
Home

Data Type Introduction


There are five simple data types, primitive types, in ECMAScript:

				Undefined, 
				Null,  
				Boolean, 
				Number, and 
				String.
				

There is one complex data type called Object, which is an unordered list of name-value pairs.

typeof Operator

Because ECMAScript is loosely typed, there needs to be a way to determine the data type of a given variable.

The typeof operator provides that information. Using the typeof operator on a value returns one of the following strings:

Output Input
"undefined" if the value is undefined
"boolean if the value is a Boolean
"string" if the value is a string
"number" if the value is number
"object" if the value is an object (other than a function) or null
"function" if the value is a function

The typeof operator is called like this:

				var message = "some string"; 
				console.log(typeof message);    //"string" 
				console.log(typeof 5);          //"number" 
				

In this example, both a variable (message) and a numeric literal are passed into the typeof operator.

typeof is an operator, no parentheses are required.

typeof sometime returns a confusing but technically correct value.

typeof null returns a value of "object", as the special value null is considered to be an empty object reference.