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() > Conceptual Overview of Using the String() Object

Conceptual Overview of Using the String() Object

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

In the code below, I detail the creation of string values in JavaScript.

Live Code

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

// create 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'
</script></body></html>

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

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

  

Start a Free 10-Day Trial


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