How to check if type is Boolean

前端 未结 16 1743
深忆病人
深忆病人 2020-12-07 10:09

How can I check if a variable\'s type is of type Boolean?

I mean, there are some alternatives such as:

if(jQuery.type(new Boolean()) === jQuery.type(         


        
相关标签:
16条回答
  • 2020-12-07 10:24

    In nodejs by using node-boolify we can use isBoolean();

            var isBoolean = require('node-boolify').isBoolean;
            isBoolean(true); //true
            isBoolean('true'); //true
            isBoolean('TRUE'); //false
            isBoolean(1); //true
            isBoolean(2); //false
            isBoolean(false); //true
            isBoolean('false'); //true
            isBoolean('FALSE'); //false
            isBoolean(0); //true
            isBoolean(null); //false
            isBoolean(undefined); //false
            isBoolean(); //false
            isBoolean(''); //false
    
    0 讨论(0)
  • 2020-12-07 10:26

    I would go with Lodash: isBoolean checks whether the passed-in variable is either primitive boolean or Boolean wrapper object and so accounts for all cases.

    0 讨论(0)
  • 2020-12-07 10:29

    If you want your function can validate boolean objects too, the most efficient solution must be:

    function isBoolean(val) {
      return val === false || val === true || val instanceof Boolean;
    }
    
    0 讨论(0)
  • 2020-12-07 10:30

    The most reliable way to check type of a variable in JavaScript is the following:

    var toType = function(obj) {
      return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
    }
    toType(new Boolean(true)) // returns "boolean"
    toType(true); // returns "boolean"
    

    The reason for this complication is that typeof true returns "boolean" while typeof new Boolean(true) returns "object".

    0 讨论(0)
  • 2020-12-07 10:31

    That's what typeof is there for. The parentheses are optional since it is an operator.

    if (typeof variable === "boolean"){
      // variable is a boolean
    }
    
    0 讨论(0)
  • 2020-12-07 10:31

    There are three "vanilla" ways to check this with or without jQuery.

    1. First is to force boolean evaluation by coercion, then check if it's equal to the original value:

      function isBoolean( n ) {
          return !!n === n;
      }
      
    2. Doing a simple typeof check:

      function isBoolean( n ) {
          return typeof n === 'boolean';
      }
      
    3. Doing a completely overkill and unnecessary instantiation of a class wrapper on a primative:

      function isBoolean( n ) {
          return n instanceof Boolean;
      }
      

    The third will only return true if you create a new Boolean class and pass that in.

    To elaborate on primitives coercion (as shown in #1), all primitives types can be checked in this way:

    • Boolean:

      function isBoolean( n ) {
          return !!n === n;
      }
      
    • Number:

      function isNumber( n ) {
          return +n === n;
      }
      
    • String:

      function isString( n ) {
          return ''+n === n;
      }
      
    0 讨论(0)
提交回复
热议问题