There are five simple data types in JavaScript:
Object is a complex data type and it is an unordered list of name-value pairs.
The typeof
operator tells the data type of a given variable.
It returns one of the following strings:
Returned Value | Meanings |
---|---|
"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 a number |
"object" | if the value is an object or null |
"function" | if the value is a function |
The typeof operator is called like this:
var message = "hi from java2s.com";
console.log(typeof message); //"string"
console.log(typeof(message)); //"string"
console.log(typeof 95); //"number"
console.log(typeof null);
var aString;
console.log(typeof aString);
In this example, both a variable (message) and a numeric literal are passed into the typeof
operator.
The typeof
is an operator and not a
function, no parentheses are required.
Calling typeof null
returns a value of "object"
since null
is considered to be an empty object reference.
The code above generates the following result.
The Undefined type has only one value, undefined
.
When a variable is declared without initialization,
it is assigned the value of undefined:
var aString;
console.log(aString == undefined); //true
In this example, the variable message is declared without initializing.
When compared with the literal value of undefined
, the two are equal.
The code above is identical to the following:
var message = undefined;
console.log(message == undefined); //true
Here the variable message is explicitly initialized to be undefined
.
This is unnecessary because any uninitialized variable gets the value of undefined.
The undefined
value is provided mainly for comparison.
The code above generates the following result.
A variable containing undefined value is different from an undefined variable.
var aString; console.log(aString); //"undefined" console.log(anotherValue); //causes an error
An undefined variable is using a variable name before its definition.
In this example, the first line displays the variable aString, which is "undefined".
In the second line, an undeclared variable called anotherValue
is passed
into the console.log() function, which causes an
error because the variable hasn't been declared.
Only one useful operation can be performed on
an undeclared variable: you can call typeof
on it.
Calling delete
on an undeclared variable won't cause an error,
but throws an error in strict mode.
typeof
returns undefined
for an uninitialized variable,
but it also returns undefined
for an undeclared variable,
which can be a bit confusing.
Consider this example:
var message; //this variable is declared but has a value of undefined console.log(typeof message); //"undefined" console.log(typeof age); //"undefined"
You should always initialize variables.
Therefore when typeof returns undefined
,
you'll know that a given variable hasn't been declared rather than was not initialized.
The null type has only one value: null
.
A null
value is an empty object pointer.
typeof
returns "object
" for
null
.
var aString = null;
console.log(typeof aString);
console.log(typeof null);
The code above generates the following result.
When defining a variable for an object, you should initialize the variable to null.
In this way, you can check for null
to determine
if the variable has an object reference.
var car = null;
console.log(typeof car); //"object"
if (car != null){
//do something with car
console.log("car is not null");
}
The code above generates the following result.
The value undefined
is a derivative of null
,
so JavaScript defines them to be equal:
console.log(null == undefined);
The code above generates the following result.
Using the equality operator (==) between null
and undefined
always returns true,
You should never explicitly set the value of a variable to undefined
.
But null should be used to initialize object variable.