This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Tap Here!
Smartphone icons created by Freepik - Flaticon
7.3 JS Typeof
In JavaScript there are 5 different data types that can contain values:
string
number
boolean
object
function
There are 6 types of objects:
Object
Date
Array
String
Number
Boolean
And 2 data types that cannot contain values:
The typeof Operator
You can use the typeof operator to find the data type of a JavaScript variable.
typeof "John" // Returns "string"
typeof 3.14 // Returns "number"
typeof NaN // Returns "number"
typeof false // Returns "boolean"
typeof [1,2,3,4] // Returns "object"
typeof {name:'John', age:34} // Returns "object"
typeof new Date() // Returns "object"
typeof function () {} // Returns "function"
typeof myCar // Returns "undefined" *
typeof null // Returns "object"
Please observe:
The data type of NaN is number
The data type of an array is object
The data type of a date is object
The data type of null is object
The data type of an undefined variable is undefined *
The data type of a variable that has not been assigned a value is also undefined *
You cannot use typeof to determine if a JavaScript object is an array (or a date).
Primitive Data
A primitive data value is a single simple data value with no additional properties and methods.
The typeof operator can return one of these primitive types:
string
number
boolean
undefined
typeof "John" // Returns "string"
typeof 3.14 // Returns "number"
typeof true // Returns "boolean"
typeof false // Returns "boolean"
typeof x // Returns "undefined" (if x has no value)
Complex Data
The typeof operator can return one of two complex types:
The typeof operator returns "object" for objects, arrays, and null.
The typeof operator does not return "object" for functions.
typeof {name:'John', age:34} // Returns "object"
typeof [1,2,3,4] // Returns "object" (not "array", see note below)
typeof null // Returns "object"
typeof function myFunc(){} // Returns "function"
The typeof operator returns "object" for arrays because in JavaScript arrays are objects.
The Data Type of typeof
The typeof operator is not a variable. It is an operator. Operators ( + - * / ) do not have any data type.
But, the typeof operator always returns a string (containing the type of the operand).
The constructor Property
The constructor property returns the constructor function for all JavaScript variables.
"John".constructor // Returns function String() {[native code]}
(3.14).constructor // Returns function Number() {[native code]}
false.constructor // Returns function Boolean() {[native code]}
[1,2,3,4].constructor // Returns function Array() {[native code]}
{name:'John',age:34}.constructor // Returns function Object() {[native code]}
new Date().constructor // Returns function Date() {[native code]}
function () {}.constructor // Returns function Function(){[native code]}
You can check the constructor property to find out if an object is an Array (contains the word "Array"):
function isArray(myArray) {
return myArray.constructor.toString().indexOf("Array") > -1;
}
Or even simpler, you can check if the object is an Array function:
function isArray(myArray) {
return myArray.constructor === Array;
}
You can check the constructor property to find out if an object is a Date (contains the word "Date"):
function isDate(myDate) {
return myDate.constructor.toString().indexOf("Date") > -1;
}
Or even simpler, you can check if the object is a Date function :
function isDate(myDate) {
return myDate.constructor === Date;
}
Undefined
In JavaScript, a variable without a value, has the value undefined . The type is also undefined .
let car; // Value is undefined, type is undefined
Any variable can be emptied, by setting the value to undefined . The type will also be undefined .
car = undefined; // Value is undefined, type is undefined
Empty Values
An empty value has nothing to do with undefined .
An empty string has both a legal value and a type.
let car = ""; // The value is "", the typeof is "string"
Null
In JavaScript null is "nothing". It is supposed to be something that doesn't exist.
Unfortunately, in JavaScript, the data type of null is an object.
You can consider it a bug in JavaScript that typeof null is an object. It should be null .
You can empty an object by setting it to null :
let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = null; // Now value is null, but type is still an object
You can also empty an object by setting it to undefined :
let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = undefined; // Now both value and type is undefined
Difference Between Undefined and Null
undefined and null are equal in value but different in type:
typeof undefined // undefined
typeof null // object
null === undefined // false
null == undefined // true
Try it yourself
The instanceof Operator
The instanceof operator returns true if an object is an instance of the specified object:
const cars = ["Saab", "Volvo", "BMW"];
(cars instanceof Array);
(cars instanceof Object);
(cars instanceof String);
(cars instanceof Number);
The void Operator
The void operator evaluates an expression and returns undefined . This operator is often used to obtain the undefined primitive value, using "void(0)" (useful when evaluating an expression without using the return value).
<a href="javascript:void(0);">
Useless link
</a>
<a href="javascript:void(document.body.style.backgroundColor='red');">
Click me to change the background color of body to red
</a>
JavaScript icons used in the buttons provided by ICONS8.COM . Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Sets Map Objects part 1
JS Sets Map Objects part 2
JS Sets Map Objects part 3
JS Sets Map Objects part 4
JS Sets Map Objects part 5
JS Sets Delete Map Objects
JS Sets Map.has() part 1
JS Sets Map.has() part 2
JS Sets Map Objects forEach()
JS Sets Map Objects Map.entries()
JS Map Objects
JS Map Objects - Map.set() part 1
JS Map Objects - Map.set() part 2
JS Map Objects Map.get()
JS Map Objects - Map.size
JS Map Objects - Map.delete()
JS Map Objects - Map.has() part 1
JS Map Objects - Map.has() part 2
JS Map Objects - Map.forEach()
JS Map Objects - Map.entries()
JS typeof Operator part 1
JS typeof Operator part 2
JS typeof Operator part 3
JS typeof Properties - constructor Property part 1
JS typeof Properties - constructor Property part 2
JS typeof Properties - Array Object
JS typeof Operator - Date Object
JS typeof Operator - Date Function
JS typeof Operator - undefined part 1
JS typeof Operator - undefined part 2
JS typeof Operator - empty values
JS typeof Operator - null
JS typeof Operator - undefined Data Type
JS instanceof Operator
JS void Operator
JS Operators - typeof Operator
JS Properties - constructor Property
JS Arrays - isArray() function part 1
JS Arrays - isArray() function part 2
JS Date Object - isDate() function part 1
JS Date Object - isDate() function part 2
JS Converting Numbers to Strings part 1
JS Converting Numbers to Strings part 2
JS typeof Operator part 1
JS typeof Operator part 2
JS Bitwise AND
JS Bitwise OR
JS Bitwise XOR
JS Bitwise NOT
JS Bitwise Left
JS Bitwise Right
JS Unsigned Bitwise Right
JS Convert Decimal to Binary
JS Convert Binary to Decimal
JS String Methods
JS Regular Expressions part 1
JS String Methods - String replace()
JS Regular Expressions part 2
JS Regular Expressions part 3
JS Error Handling part 1
JS try catch
JS Errors - RangeError
JS Errors - ReferenceError
JS Errors - SyntaxError
JS Errors - TypeError
JS Errors - URIError
JS Scope - Local Variables
JS Scope - GLOBAL Variable
JS Global Variables
JS Scope - Global HTML variables
JS Hoisted Declarations part 1
JS Hoisted Declarations part 2
JS Hoisting part 1
JS Hoisting part 2
JS Initializations are Not Hoisted part 1
JS Initializations are Not Hoisted part 2
JS Initializations are Not Hoisted part 3
JS "use strict" Mode part 1
JS "use strict" Mode part 2
JS this Keyword
JS this Keyword [object Window] part 1
JS this Keyword - window object
JS this Keyword [object Window] part 2
JS this Keyword - Function
JS this Keyword - Event Handler
JS this Keyword - Event Handler
JS this Keyword - person object part 1
JS this Keyword - person object part 2
JS this Keyword - Explicit Function Binding
JS this Keyword - Function bind()
JS this Keyword - Arrow Function part 1
JS this Keyword - Arrow Function part 2