Student iconAre you a student? Get a yearly Tuts+ subscription for $45 →
This post is part of a series called JavaScript Succinctly.
Working With Objects and Properties

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

<!DOCTYPE html><html lang="en"><body><script>

	// Create a string object using the new keyword and the String() constructor.
	var stringObject = new String('foo');
	console.log(stringObject); // Logs foo {0 = 'f', 1 = 'o', 2 = 'o'}
	console.log(typeof stringObject); // Logs 'object'.

	// Create string literal/primitive by directly using the String constructor.
	var stringObjectWithOutNewKeyword = String('foo'); // Without new keyword.
	console.log(stringObjectWithOutNewKeyword); // Logs 'foo'.
	console.log(typeof stringObjectWithOutNewKeyword); // Logs 'string'.

	// Create string literal/primitive (constructor leveraged behind the scene).
	var stringLiteral = 'foo';
	console.log(stringLiteral); // Logs foo.
	console.log(typeof stringLiteral); // Logs 'string'.


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

<!DOCTYPE html><html lang="en"><body><script>

	// Create string object.
	var stringObject = new String('foo');

	console.log(stringObject); // Logs 'foo {0="f", 1="o", 2="o"}'


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();)


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