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 Arrays
So far, you've used arrays of numbers. JavaScript also allows you to use string arrays, or arrays of strings. This is a powerful feature that allows you to work with a large number of strings at the same time.
Creating a String Array
You declare a string array in the same way as a numeric array—in fact, JavaScript does not make a distinction between them:
names = new Array(30);
You can then assign string values to the array elements:
names[0] = "Henry J. Tillman"; names[1] = "Sherlock Holmes";
As with numeric arrays, you can also specify a string array's contents when you create it. Either of following statements would create the same string array as the above example:
names = new Array("Henry J. Tillman", "Sherlock Holmes");
names = ["Henry J. Tillman", "Sherlock Holmes"];
You can use string array elements anywhere you would use a string. You can even use the string methods introduced earlier. For example, the following statement prints the first five characters of the first element of the names array, resulting in Henry:
document.write(names[0].substring(0,5));
Splitting a String
JavaScript includes a string method called split, which splits a string into its component parts. To use this method, specify the string to split and a character to divide the parts:
test = "John Q. Public";
parts = test.split(" ");
In this example, the test string contains the name John Q. Public. The split method in the second statement splits the name string at each space, resulting in three strings.
These are stored in a string array called parts. After the example statements execute, the elements of parts contain the following:
- parts[0] = "John"
- parts[1] = "Q."
- parts[2] = "Public"
JavaScript also includes an array method, join, that performs the opposite function. This statement reassembles the parts array into a string:
fullname = parts.join(" ");
The value in the parentheses specifies a character to separate the parts of the array. In this case, a space is used, resulting in the final string John Q. Public. If you do not specify a character, commas are used.
Sorting a String Array
JavaScript also includes a sort method for arrays, which returns an alphabetically sorted version of the array. For example, the following statements initialize an array of four names and sort it:
names[0] = "Public, John Q."; names[1] = "Tillman, Henry J."; names[2] = "Bush, George W."; names[3] = "Mouse, Mickey"; sortednames = names.sort();
The last statement sorts the names array and stores the result in a new array, sortednames.
Sorting a Numeric Array | Next Section

Account Sign In
View your cart