Search notes:

JavaScript: operator typeof

typeof(obj) returns a String that describes the type of obj.
typeof does not throw an error if obj does not exist at all, it just returns the string "undefined".
The possible returned values are:
Since the set of return values is rather small, a better way to find out with what object someone is dealing, obj.constructor.name might be more constructive.
<!DOCTYPE HTML>
<html>
<head>
  <title>typeof operator</title>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">

  <script type="text/javascript">

    var out;

    function show_typeof(thing) {

       out.innerHTML += "typeof(" + thing + ") =  " + (typeof thing)  + "<br>";

    }

    function init() {

      out = document.getElementById('out');


      show_typeof(        42 ); // number
      show_typeof(       NaN ); // number
      show_typeof("forty-two"); // string
      show_typeof(      null ); // object
      show_typeof(document   ); // object
      show_typeof(arguments  ); // object
      show_typeof(      true ); // boolean
      show_typeof(show_typeof); // function
      show_typeof(    /re/   ); // object
      show_typeof(Infinity   ); // number
      show_typeof(undefined  ); // undefined
      show_typeof(console    ); // object
      show_typeof(Number     ); // function

      out.innerHTML += "typeof(foo_bar_baz) = " + typeof(foo_bar_baz) + "<br>"; // undefined
      
    }
  </script>
</head>
<body onload="init();">

  <div id="out"></div>

  <h1>Links</h1>

    <a href='http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/'>Fixing the JavaScript typeof operator</a>.

</body>
</html>

Github repository about-javascript, path: /operators/typeof.html

See also

Operators
The VBA operator typeOf() … is …

Index