what is the best way to check variable type in javascript

后端 未结 6 793
天涯浪人
天涯浪人 2020-12-15 22:41


        
6条回答
  •  一生所求
    2020-12-15 23:08

    The best way is to use the typeof keyword.

    typeof "hello" // "string"
    

    The typeof operator maps an operand to one of six values: "string", "number", "object", "function", "undefined" and "boolean". The instanceof method tests if the provided function's prototype is in the object's prototype chain.

    This Wikibooks article along with this MDN articles does a pretty good job of summing up JavaScript's types.

提交回复
热议问题