Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

10. String() > String() Parameters

String() Parameters

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

Live Code

<!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"}'

</script></body></html>

Note

Instances from the String() constructor, when used with the new keyword, produce an actual complex object. You should avoid doing this (using 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.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint