Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 782185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:20:51+00:00 2026-05-14T20:20:51+00:00

I need to check the type of a variable in JavaScript. I know 3

  • 0

I need to check the type of a variable in JavaScript. I know 3 ways to do it:

  1. instanceof operator: if(a instanceof Function)

  2. typeof operator: if(typeof a=="function"

  3. toString method (jQuery uses this): Object.prototype.toString.call(a) == "[object Function]"

Which is the most accurate way to do type checking beetween these solutions? And why? Please don’t tell me that the last solution is better only because jQuery uses that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:20:52+00:00Added an answer on May 14, 2026 at 8:20 pm

    [Edit 2023/05/28] See this github repository for a more comprehensive type checker.

    How about my home brew function to determine variable ‘type’? It can also determine the type of custom Objects:

    function whatType(somevar){
        return String(somevar.constructor)
                .split(/\({1}/)[0]
                .replace(/^\n/,'').substr(9);
    }
    var num = 43
        ,str = 'some string'
        ,obj = {}
        ,bool = false
        ,customObj = new (function SomeObj(){return true;})();
    
    alert(whatType(num)); //=>Number
    alert(whatType(str)); //=>String
    alert(whatType(obj)); //=>Object
    alert(whatType(bool)); //=>Boolean
    alert(whatType(customObj)); //=>SomeObj
    

    Based on the constructor property of variables you could also do:

    function isType(variable,type){
     if ((typeof variable).match(/undefined|null/i) || 
           (type === Number && isNaN(variable)) ){
            return variable
      }
      return variable.constructor === type;
    }
    /** 
     * note: if 'variable' is null, undefined or NaN, isType returns 
     * the variable (so: null, undefined or NaN)
     */
    
    alert(isType(num,Number); //=>true
    

    Now alert(isType(customObj,SomeObj) returns false. But if SomeObj is a normal Constructor function, it returns true.

    function SomeObj(){return true};
    var customObj = new SomeObj;
    alert(isType(customObj,SomeObj); //=>true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.