What is a constructor to create String object in JavaScript?


To create a String object in JavaScript, use string.constructor. A constructor returns a reference to the string function that created the instance's prototype.

Example

You can try to run the following code to create a String object with the constructor −

Live Demo

<html>
   <head>
      <title>JavaScript String constructor Method</title>
   </head>

   <body>
      <script>
         var str = new String( "This is string" );
         document.write("str.constructor is:" + str.constructor);
      </script>
   </body>
</html>

Updated on: 15-Jun-2020

72 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements