How to check if type is Boolean

前端 未结 16 1764
深忆病人
深忆病人 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条回答
  •  萌比男神i
    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
    }
    

提交回复
热议问题