Loading Please Wait...
The typeof operator return the data type of the variables.
JavaScript has 5 different data types:
JavaScript has 6 types of object:
JavaScript has 3 data types that can not contain values:
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 that:
We cannot use typeof to determine if a JavaScript object is an array (or a date).
The constructor property returns the constructor function for all JavaScript variables. This constructor function can be used to check for data types.
"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]}
function isArray(myArray) {
return myArray.constructor.toString().indexOf("Array") > -1;
}
// OR
function isArray(myArray) {
return myArray.constructor === Array;
}
function isDate(myDate) {
return myDate.constructor.toString().indexOf("Date") > -1;
}
// OR
function isDate(myDate) {
return myDate.constructor === Date;
}
In JavaScript, a variable without a value, has the value undefined. The type is also undefined.
Any variable can be emptied, by setting the value to undefined. The type will also be undefined.
let car; // Value is undefined, type is 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
The instanceof operator returns true if an object is an instance of the specified object
const cars = ["Saab", "Volvo", "BMW"];
(cars instanceof Array); // true
(cars instanceof Object); // true
(cars instanceof String); // false
(cars instanceof Number); // false
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>
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved