Sams Teach Yourself JavaScript in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Part I: Getting Started
- Hour 1. Understanding JavaScript
- Hour 2. Creating a Simple Script
- Hour 3. How JavaScript Programs Work
- Part II: Learning JavaScript Basics
- Hour 4. Using Functions and Variables
- Hour 5. Using Strings and Arrays
- Hour 6. Testing and Comparing Values
- Hour 7. Repeating Yourself: Using Loops
- Hour 8. Using Math and Date Functions
- Part III: The Document Object Model (DOM)
- Hour 9. Working with the Document Object Model
- Hour 10. Responding to Events
- Hour 11. Using Windows and Frames
- Hour 12. Getting Data with Forms
- Hour 13. Using Graphics and Animation
- Part IV: Moving on to Advanced JavaScript Features
- Hour 14. Creating Cross-Browser Scripts
- Hour 15. Creating Custom Objects
- Hour 16. Working with Sounds and Plug-Ins
- Hour 17. Debugging JavaScript Applications
- Part V: Working with Dynamic HTML (DHTML)
- Hour 18. Working with Style Sheets
- Hour 19. Using Dynamic HTML (DHTML)
- Hour 20. Using Advanced DOM Features
- Part VI: Putting It All Together
- Hour 21. Improving a Web Page with JavaScript
- Hour 22. Creating a JavaScript Game
- Hour 23. Creating DHTML Applications
- Hour 24. JavaScript Tips and Tricks
- Part VII: Appendices
- Appendix A. Other JavaScript Resources
- Appendix B. Tools for JavaScript Developers
- Appendix C. Glossary
- Appendix D. JavaScript Quick Reference
- Appendix E. DOM Quick Reference
Converting Between Data Types
JavaScript handles conversions between data types for you whenever it can. For example, you've already used statements like this:
document.write("The total is " + total);
This statement prints out a message such as "The total is 40." Since the document.write function works with strings, the JavaScript interpreter automatically converts any non-strings in the expression (in this case, the value of total) to strings before performing the function.
This works equally well with floating-point and boolean values. However, there are some situations where it won't work. For example, the following statement will work fine if the value of total is 40:
average = total / 3;
However, the total variable could also contain a string; in this case, the statement above would result in an error.
In some situations, you may end up with a string containing a number, and need to convert it to a regular numeric variable. JavaScript includes two functions for this purpose:
- parseInt() converts a string to an integer number.
- parseFloat() converts a string to a floating-point number.
Both of these functions will read a number from the beginning of the string, and return a numeric version. For example, these statements convert the string "30 angry polar bears" to a number:
stringvar = "30 angry polar bears"; numvar = parseInt(stringvar);
After these statements execute, the numvar variable contains the number 30. The non-numeric portion of the string is ignored.
Workshop: Storing User Data in Variables | Next Section

Account Sign In
View your cart