64x64 icon dark hosting
Deploy New Relic now and get $135 off your Tuts+ subscription.
Advertisement

String()

by
This post is part of a series called JavaScript Succinctly.
Working With Objects and Properties
Number()

The String() constructor function is used to create string objects and string primitive values.

In the following sample, I detail the creation of string values in JavaScript.

Sample: sample47.html


String() Parameters

The String() constructor function takes one parameter: the string value being created. In the following sample, we create a variable, stringObject, to contain the string value "foo".

Sample: sample48.html

When used with the new keyword, instances from the String() constructor produce an actual complex object. You should avoid doing this (use literal/primitive numbers) due to the potential problems associated with the typeof operator. The typeof operator reports complex string objects as 'object' instead of the primitive label ('string') you might expect. Additionally, the literal/primitive value is just faster to write and is more concise.


String() Properties and Methods

The String object has the following properties and methods (not including inherited properties and methods):

Properties (e.g., String.prototype;)

Methods (e.g., String.fromCharChode();)


String() Object Instance Properties and Methods

String object instances have the following properties and methods (not including inherited properties and methods):

Instance Properties (e.g., var myString = 'foo'; myString.length;)

Instance Methods (e.g., var myString = 'foo'; myString.toLowerCase();)


Conclusion

So that details the process of creating a string using the String() constructor, its methods and properties.

Advertisement