HTML and CSS Reference
var string_name = "string of text";
var string_name = new String("string of text");
<head><title>The String Object</title></head>
<h2>Primitive and String Objects</h2>
var first_string = "The winds of war are blowing.";
var next_string = new String("There is peace in the valley.");
document.write("The first string is of type<em> "+
document.write(".</em><br />The second string is of type<em> "
+ typeof(next_string) +".<br />");
This is the literal way to assign a string to a variable, and the most typical way. The
string is called a string primitive. It is one of the basic building blocks of the lan-
guage, along with numbers and Booleans. All of the properties and methods of the
String object behave the same way whether you create a String literal or a String
object as shown next. For all practical purposes, both methods of creating a string
are the same, though this one is the easiest.
The String() constructor and the new keyword are used to create a String object.
This is the explicit way of creating a string.
The typeof operator demonstrates that the first string, created the literal, implicit
way, is a String data type.
The typeof operator demonstrates that this string, created with the String() con-
structor, is an object type. Either way, when properties and methods are applied
to a string, it is treated as a String object (see Figure 9.27).