JavaScript tutorial:
String object

 

Methods

Properties

The String object allows manipulation and formatting of text strings and determination and location of substrings within strings.

Syntax

StringObj[.method]
"String Literal"[.method]

Example

String objects can be created implicitly using string literals. String objects created in this fashion (referred to as standard strings) are treated differently than String objects created using the new operator. All string literals share a common, global string object. So, if a property is added to a string literal, it is available to all standard string objects:

alpha = "This is a string";
document.write(alpha);

In this example, test is now defined for beta and all future string literals. In the following example, however, added properties are treated differently:

gamma = new String("This is a string");
delta = new String("This is also a string");
gamma.test = 10;
document.write(gamma.test);

  To run the code above, paste it into JavaScript Editor, and click the Execute button.

In this case, test is not defined for delta. Each String object declared as a new String object has its own set of members. This is the only case where String objects and string literals are handled differently.

See also: new Operator