About This Product
FontsMadeEasy.com
 
Search This Database:
| Over 5000 Free Fonts | Tutorials | Javascript Forum | Other Javascript Resources | Cheat Sheet
Tutorial archive
General Questions
Javascript 1.2
Navigation
Numbers
Strings
Object Model
Reference Manual
Dialog
Windows
Frames
Forms
Images
Mouse Events
Colors
File Access
Control Status Bar
Dates and Time
Cookies
Client Information
Bookmarklets


Number vs. String

Question: Is there a way to test whether a particular variable holds a number or a string?

Answer: Yes. To test whether the variable holds a number or a string, use the typeof operator. If your variable holds a number, typeof(variable) will return "number". If it holds a string, typeof(variable) will return "string". The following are examples of typeof usage:

typeof(123)    // result: "number"
typeof("123")  // result: "string"

if (typeof k == "string") { alert('k is a string.') }
if (typeof k == "number") { alert('k is a number.') }
The typeof operator can also help you distinguish between other data types. Depending on the particular variable's value, the result of typeof can be one of the following:
    "number"
    "string"
    "boolean"
    "function"
    "object"
    "undefined"

BackBack