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
Using String Objects
You've already used several strings during the first few hours of this book. Strings store a group of text characters, and are named similarly to other variables. As a simple example, this statement assigns the string This is a test to a string variable called test:
test = "This is a test";
Creating a String Object
JavaScript stores strings as String objects. You usually don't need to worry about this, but it will explain some of the techniques for working with strings, which use methods (built-in functions) of the String object.
There are two ways to create a new String object. The first is the one you've already used, while the second uses official object syntax. The following two statements create the same string:
test = "This is a test";
test = new String("This is a test");
The second statement uses the new keyword, which you use to create objects. This tells the browser to create a new String object containing the text This is a test, and assigns it to the variable test.
Assigning a Value
You can assign a value to a string in the same way as any other variable. Both of the examples in the previous section assigned an initial value to the string. You can also assign a value after the string has already been created. For example, the following statement replaces the contents of the test variable with a new string:
test = "This is only a test.";
You can also use the concatenation operator (+) to combine the values of two strings. Listing 5.1 shows a simple example of assigning and combining the values of strings.
Example 5.1. Assigning values to strings and combining them
<html> <head> <title>String Test</title> </head> <body> <h1>String Test</h1> <script LANGUAGE="JavaScript" type="text/javascript">; test1 = "This is a test. "; test2 = "This is only a test."; both = test1 + test2; alert(both); </script> </body> </html>
This script assigns values to two string variables, test1 and test2, and then displays an alert with their combined value. If you load this HTML document in a browser, your output should resemble Figure 5.1.
Figure 5.1 The output of the string example script.
In addition to using the + operator to concatenate two strings, you can use the += operator to add text to a string. For example, this statement adds a period to the current contents of the string sentence:
sentence += ".";
Calculating the String's Length
From time to time, you may find it useful to know how many characters a string variable contains. You can do this with the length property of String objects, which you can use with any string. To use this property, type the string's name followed by .length.
For example, test.length refers to the length of the test string. Here is an example of this property:
test = "This is a test."; document.write(test.length);
The first statement assigns the string This is a test to the test variable. The second statement displays the length of the string—in this case, 15 characters.
Converting the String's Case
Two methods of the String object allow you to convert the contents of a string to all uppercase or all lowercase:
- toUpperCase() converts all characters in the string to uppercase.
- toLowerCase() converts all characters in the string to lowercase.
For example, the following statement displays the value of the test string variable in lowercase:
document.write(test.toLowerCase());
Assuming that this variable contained the text This Is A Test, the result would be the following string:
this is a test
Note that the statement doesn't change the value of the text variable. These methods return the upper- or lowercase version of the string, but they don't change the string itself. If you want to change the string's value, you can use a statement like this:
test = test.toLowerCase();
Working with Substrings | Next Section

Account Sign In
View your cart