Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript typeof

JS typeof

The typeof operator return the data type of the variables.

JS Data Types

JavaScript has 5 different data types:

  • string
  • number
  • boolean
  • object
  • function

JavaScript has 6 types of object:

  • Object
  • Date
  • Array
  • String
  • Number
  • Boolean

JavaScript has 3 data types that can not contain values:

  • null
  • undefined
  • NaN
					 
        
          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"
        
      
Notable Consideration

Please observe that:

  • 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

We cannot use typeof to determine if a JavaScript object is an array (or a date).

The Constructor Property

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]}
        
      
Check For Array
					 
        
          function isArray(myArray) {
            return myArray.constructor.toString().indexOf("Array") > -1;
          }

          // OR
          function isArray(myArray) {
            return myArray.constructor === Array;
          }
        
      
Check For Date
					 
        
          function isDate(myDate) {
            return myDate.constructor.toString().indexOf("Date") > -1;
          }

          // OR
          function isDate(myDate) {
            return myDate.constructor === Date;
          }
        
      
Undefined

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
        
      
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
        
      
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);   // true
          (cars instanceof Object);  // true
          (cars instanceof String);  // false
          (cars instanceof Number);  // false
        
      
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>
        
      

How you feel about this blog:

Share this blog on:

Report Us

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 Us
Ads
Logo
Lynxsia IT Solutions

We 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..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved