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
What Is an Object?
As you know from earlier hours of this book, objects allow you to combine several kinds of data (properties) and functions to act on the data (methods) into a single, convenient package. In this hour, you'll learn more about the built-in Math and Date objects—but first, here's a quick overview of the way objects work in JavaScript.
Creating Objects
Each object has a special function, called a constructor, that's used to create objects. For example, JavaScript includes a built-in function called String to create String objects. That's why you can create a string variable like this:
myname=new String("Figby");
The new keyword tells JavaScript to create a new object—or in technical terms, a new instance of the String object. This particular instance will have the value Figby and will be stored in the variable myname.
You can use the same basic syntax to create String objects, Date objects, Array objects, and even your own custom objects. (The Math object is an exception, which you'll learn about later in this hour.)
Object Properties and Values
Each object can have one or more properties, or attributes. Each property is basically a variable in itself, and is contained within the object. Each property can be assigned a value. You can use properties to store any type of data a variable can store.
You've already used a few object properties, such as the length property of strings and arrays. To refer to a property, you use the object's name, a period, and the property's name. For example, the length of the names array is referred to with this property:
names.length
Object properties can even contain objects themselves. For example, each of an array's elements is a special type of property, named with an index value. If the names array contains strings, it is an array of String objects. Here is the syntax for the length of the first element of names:
names[1].length
Understanding Methods
As you learned in Hour 4, "Using Functions and Variables," functions are combinations of statements that can be executed as a single unit. Methods are functions that are stored as properties of an object.
You've already used methods. For example, you've used the write method of the document object to write content to a Web page. Here is an example:
document.write("Hello there.");
Like ordinary functions, methods can optionally return a value. For example, this statement rounds a number using the round method of the Math object, which you'll learn more about later, and stores the result in the variable final:
final = Math.round(num);
Using the with Keyword
The with keyword is one you haven't seen before. You can use it to make JavaScript programming easier—or at least easier to type.
The with keyword specifies an object, and it is followed by a block of statements enclosed in braces. For each statement in the block, any properties you mention without specifying an object are assumed to be for that object.
As an example, suppose you have a string called lastname. You can use with to perform string operations on it without specifying the name of the string every time:
with (lastname) {
window.alert("length of last name: " + length);
toUpperCase();
}
In this example, the length property and the toUpperCase method refer to the lastname string, although it is only specified once with the with keyword.
Obviously, the with keyword only saves a bit of typing in situations like this. However, you will find it very useful when you're dealing with an object throughout a large procedure, or when you are using a built-in object, such as the Math object.
The Math Object | Next Section

Account Sign In
View your cart