The String
object is by far the most commonly used object in JavaScript. JavaScript automatically creates a String
object for you anytime you define a variable that has a string data type. For example:
var myStr = "Teach Yourself jQuery & JavaScript in 24 Hours";
When you create a string, there are a few special characters that you can’t add directly to the string. For those characters, JavaScript provides a set of escape codes, listed in Table 1.5.
To determine the length of a string, you can use the length
property of the String
object, as in this example:
var numOfChars = myStr.length;
The String
object has several functions that enable you to access and manipulate the string in various ways. The methods for string manipulation are described in Table 1.6.
To get you started on using the functionality provided in the String
object, the following sections describe some of the common tasks that can be done using String
object methods.