JavaScript 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:
null
undefined
The typeof Operator
You can use the typeof
operator to find the data type of a JavaScript variable.
Example
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
Example
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:
function
object
The typeof
operator returns “object” for objects, arrays, and null.
The typeof
operator does not return “object” for functions.
Example
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.
Example
"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”):
Example
function isArray(myArray) {
return myArray.constructor.toString().indexOf("Array") > -1;
}
Or even simpler, you can check if the object is an Array function:
Example
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”):
Example
function isDate(myDate) {
return myDate.constructor.toString().indexOf("Date") > -1;
}
Or even simpler, you can check if the object is a Date function:
Example
function isDate(myDate) {
return myDate.constructor === Date;
}
Undefined
In JavaScript, a variable without a value, has the value undefined
. The type is also undefined
.
Example
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
.
Example
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.
Example
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
:
Example
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
:
Example
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
41 Responses
levofloxacin 250mg tablet levaquin 500mg sale
purchase dutasteride online cheap zofran 8mg cheap brand zofran 8mg
spironolactone cheap buy propecia 1mg for sale oral fluconazole 100mg
cheap ampicillin buy erythromycin 500mg pills buy erythromycin for sale
buy sildenafil 50mg online sildenafil 50mg drug order robaxin 500mg sale
buy sildenafil 50mg without prescription order sildenafil 50mg generic estrace 2mg pill
lamictal medication order tretinoin cream online cheap order tretinoin cream without prescription
tadalafil medication tadacip order order voltaren 100mg sale
accutane 20mg pills order amoxil generic order zithromax 250mg online
indomethacin 50mg pill suprax 100mg uk purchase trimox pill
buy tadalafil Cost viagra sildenafil tablet
buy arimidex pill generic clarithromycin 250mg sildenafil tablet
tadalafil gГ©nГ©rique tadalafil 40mg comprimГ© viagra 25mg pour homme
order deltasone 10mg pill compare price 20 mg cialis cost sildenafil
tadalafil 5mg kaufen generika tadalafil 10mg ohne rezept viagra für männer
cost accutane 40mg buy azithromycin 500mg online cheap ivermectin 6mg for humans
order doxycycline pills lasix pills furosemide 100mg tablet
cheap ramipril 5mg purchase ramipril without prescription buy azelastine 10 ml online cheap
buy generic catapres tiotropium bromide buy online order tiotropium bromide online
buspar 5mg without prescription brand amiodarone oxybutynin drug
oral terazosin 5mg pioglitazone ca order azulfidine without prescription
alendronate buy online panadol online pepcid online buy
olmesartan online buy order generic acetazolamide 250mg order diamox 250 mg generic
oral prograf buy ropinirole without prescription cost urso 300mg
order imdur 20mg pills purchase telmisartan generic telmisartan 80mg drug
buy zyban 150mg pill buy cetirizine 10mg quetiapine 100mg price
molnunat 200 mg brand buy molnunat 200 mg sale buy prevacid online cheap
generic zoloft 50mg zoloft pills buy viagra 100mg
buy tadalafil 20mg sale tadalafil 10mg brand cheap generic sildenafil
over the counter cialis order amantadine for sale buy amantadine sale
naltrexone medication aripiprazole without prescription buy aripiprazole generic
buy dapsone 100mg without prescription purchase dapsone sale order aceon generic
order provera online cheap buy provera pill order periactin 4 mg
modafinil pill provigil tablet buy ivermectin 6mg for humans
order luvox 100mg for sale order nizoral without prescription glipizide 5mg over the counter
buy isotretinoin 40mg online isotretinoin order online generic prednisone 40mg
order azithromycin 500mg order gabapentin 800mg buy gabapentin 100mg generic
order lasix 100mg pills furosemide for sale hydroxychloroquine uk
buy tadalafil without prescription cheap tadalafil without prescription anafranil 25mg sale
where to get viagra over the counter I Deletional analysis of the 500 bp mouse Ins2 promoter region by 100 bp increments using the Luc PEST reporter assay in MIN6 cells starved from aas and restimulated for 2 h
Effect of Position finasteride sample Monitor Closely 1 carbamazepine will decrease the level or effect of hydrocodone by affecting hepatic intestinal enzyme CYP3A4 metabolism