HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
In JavaScript there are 5 different data types that can contain values
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:
You cannot use typeof
to determine if a JavaScript object is an array (or a date).
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)
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.
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 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 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:
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:
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
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"
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
undefined
and null
are equal in value but different in type:
typeof undefined
// undefined
typeof null
// object
null === undefined
// false
null == undefined
// true