01/01/2021 2 min read

JavaScript Strings

Learn how to work with text in JavaScript.

#javascript
Edit on GitHub

What is the difference between string literals and string objects in JavaScript?

String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings. JavaScript automatically converts primitives to String objects, so that it’s possible to use String object methods for primitive strings. In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.

Explanation

Whenever we try to access a property/method on a primitive string, JavaScript automatically converts the primitive string into a string object. So, 'Saabbir'.length converts into new String('Saabbir').length behind the scene. That’s why we can access all of those string methods on a primitive string like Saabbir.

What is a string object?

Strings created from String constructor function in a constructor context (i.e, using the new keyword) are string object.

Example

new String("Saabbir Hossain");

Key takeaways

  • Without using new operator, String function calls always returns string primitive value.

  • String function takes any types of data as an argument and converts it into it’s corresponding primitive string value. So, String(100) will return the primitive string "100". In here, the number value 100 gets converted into its corresponding "100" string value.

String prototype chain

new String("string").__proto__ === String.prototype;
String.prototype.__proto__ === Object.prototype;
Object.prototype.__proto__ === null;
Saabbir Hossain

Written by Saabbir Hossain

Sr. Software Engineer at EchoLogyx Ltd, CRO-driven Shopify Plus Developer and Optimizely Certified A/B Testing Expert based in Bangladesh.

Discussion & Comments

WhatsApp Chat Send Email