Working with JavaScript Strings: Introduction 1/12

Working with JavaScript Strings: Introduction 1/12

JavaScript strings are the most important component of JavaScript, probably used morethan any other data type. Though you may get numeric values from web page forms,the values are retrieved as strings, which you then have to convert into numeric values.

Strings are also used as parameters when invoking server-side application calls throughAjax, as well as forming the basic serialization format of every JavaScript object. Oneof the methods that all JavaScript objects share is toString, which returns a stringcontaining the serialized format of the object.



A String Primitive

A JavaScript string can be both a primitive data type or an object. As a primitive type,it joins with four other JavaScript primitive types: number, Boolean (true or false), null (no value), and undefined (unknown). In addition, as a primitive data type, stringsare also JavaScript literals: a collection that includes numbers (as either floating pointor integer), the literal format for arrays, objects, and regular expressions, as well asnumbers and Booleans.

We’ll see more about the literal formats for the various JavaScript ob-jects throughout the article.

A string is zero or more characters delimited by quotes, either single quotes:

'This is a string'


Or double quotes:

"This is a string"


There is no rule for which type of quote to use. If you’re including single quotes withinthe text, you’ll most likely want to use double quotes:

"This isn't a number."


If you mix up the quote types—begin a string with a single quote and end with adouble—you’ll receive an application error:

var badString = 'This is a bad string"; // oops, error


Both quote types are used interchangeably in the article.

A String Object

A string object is called String, appropriately enough, and like all other JavaScriptobjects has a set of properties that come prebuilt into the object type.

A String object can be instantiated using the JavaScript new operator, to create a newobject instance:

var city = new String("St. Louis");


Once instantiated, any one of the available string properties can be accessed on it, suchas in the following code, where the string is lowercased using the String object methodtoLowerCase:

var lcCity = city.toLowerCase(); //  new string is now st. louis


If you access the String constructor without using new, you’ll create a string literal ratherthan a String object:

var city = String("St. Louis");


If you do need to access a String object method on a string literal, you can. Whathappens is the JavaScript engine creates a String object, wraps it around the stringliteral, performs the method call, and then discards the String object.

When to use String, as compared to using a string literal, depends on the circumstan-ces. Unless you plan on using the String object properties, you’ll want to use stringliterals wherever possible. However, if you’ll be using String methods, then create thestring as an object.

See Also

Mozilla has a terrific page that discusses the concept of JavaScript literals and the dif-ferent types.  You can access the page at Literals.

Comments

Popular posts from this blog

Working with JavaScript Strings: Conditionally Comparing Strings 4/12

Working with JavaScript Strings: Concatenating Two or More Strings 2/12

Working with JavaScript Strings: Finding a Substring in a String 5/12