String Wrapper

String in JavaScript

String Wrapper

🙋 Need help? Ask an expert now!

Every primitive type has a object wrapper type, just like how the data type number has the object wrapper Number(). As mentioned previously, primitive data types are passed by value while objects are passed by reference. In fact, since primitive data types can't hold properties, the JavaScript engine converts the primitive data type into its corresponding object type before calling the property of the string. You can use new String() to explicitly create string object or use String(stringObject) to create explicitly create a primitive string.

The simplest way to declare a primitive string is using single or double quotes. Creating an object requires the use of the keyword new String() with a parameter. If you ever want to explicitly declare a primitive string, you can also use the String(stringObject) as shown above to perhaps convert a string object to a primitive data type. The property of all three of these strings can be accessed in the same way using the dot notation as shown above.

Edit Me on GitHub!

Application Question

Consider the following code segment:

var stringOne = new String ("Hello");
var stringTwo = "Hello";
var stringThree = new String (stringOne);
var stringFour = String (stringTwo);

Which string are primitive strings?