Introduction to JavaScript

Using typeof()

 

You can use the typeof() operator to examine an "operand" (a string, variable, method, function, keyword, object, or property). typeof is an unusual operator because it is not represented by punctuation characters but instead by the typeof keyword. It is a unary operator that is placed before a single operand, which can of any type. The typeof() operator will return a string that describes the type of operand. In other words, the typeof() operator will tell you what sort of string, variable, method, function, keyword, object, or property you have. This might be useful if want to carry out the operation (so you can select the appropriate operation depending on the type of the operand).

typeof syntax can take one of two forms:

  • typeof null

-- OR --

  • typeof(null)


<SCRIPT>

var sSize = "big";
var nSize = 1;
var todayDate = new Date();

</SCRIPT>

<BODY>

<SCRIPT>

document.write("sSize = " + typeof(sSize) + " <BR>")
document.write("nSize = " + typeof(nSize) + " <BR>")
document.write("todayDate = " + typeof(todayDate) + " <BR>")
document.write("nothingAtAll = " + typeof(nothingAtAll) + " <BR>")
document.write("null = " + typeof(null) + " <BR>")
document.write("false = " + typeof(false) + " <BR>")
document.write("true = " + typeof(true) + " <BR>")
document.write("666 = " + typeof(666) + " <BR>")
document.write("A little bit of text = " + typeof('A little bit of text') + " <BR>")
document.write("document.linkColor = " + typeof(document.linkColor) + " <BR>")
document.write("document.lastModified = " + typeof(document.lastModified) + " <BR>")
document.write("document.anchors = " + typeof(document.anchors) + " <BR>")
document.write("window.history = " + typeof(window.history) + " <BR>")
document.write("window.length = " + typeof(window.length) + " <BR>")
document.write("Math.E = " + typeof(Math.E) + " <BR>")
document.write("blur = " + typeof(blur) + " <BR>")
document.write("Date = " + typeof(Date) + " <BR>")
document.write("String = " + typeof(String) + " <BR>")

</SCRIPT>