Do any technical differences have the following two methods for setting string variables in JavaScrpit ?

 // 1 var s1= "JavaScript"; // 2 var s2 = new String("JavaScript"); 
  • of course there is, s2 will be an object. s1 - line - Vasily Barbashev
  • @ Vasily Barbashev, if so, then for s1 , the .length method should not work, and it works like it used to be s1 - object - Bokov Gleb
  • @GurebuBokofu, not exactly :) - Grundy
  • And detail? :) - Hokov Gleb

2 answers 2

You can start with MDN :

Difference between string primitives and string objects

Important: Javascript contains differences between string objects and primitives (This is true for Boolean and Number )

String literals (denoted by double or single quotes) and strings returned by calling String in a non-design context (that is, without the new keyword) are string primitives.
Javascript automatically converts primitives to String objects, so it is possible to use the methods of the String object for string primitives. In situations where you need to call a method or find a property for a primitive string, Javascript will automatically wrap the primitive into an object and call the desired method or property.

 var s_prim = 'foo'; var s_obj = new String(s_prim); console.log(typeof s_prim); // Logs "string" console.log(typeof s_obj); // Logs "object" 

Line primitives and string objects have a difference when using eval() . The primitives passed to eval will be processed as source code; Row objects will be processed like all other objects by returning themselves, for example:

 var s1 = '2 + 2'; // creates a string primitive var s2 = new String('2 + 2'); // creates a String object console.log(eval(s1)); // returns the number 4 console.log(eval(s2), s2 === eval(s2)); // returns the string "2 + 2" 

Therefore, the code may break if it receives a String object as input, instead of the expected primitive string, however, you usually do not need to worry about the differences.

you can always get the corresponding primitive from an object string by calling the valueOf method.

In addition: literals are immutable, therefore, unlike objects, they cannot add new properties, as well as their comparison occurs as a comparison of objects

 var primitive_s = '123'; var primitive_s2 = '123' var s1 = new String('123'); var s2 = new String('123'); primitive_s.prop = 10; console.log('primitive_s.prop', primitive_s.prop); s1.prop = 10; console.log('s1.prop', s1.prop); console.log(primitive_s == primitive_s2, primitive_s === primitive_s2); console.log(s1 == s2, s1 === s2); // но! console.log(primitive_s == s1, primitive_s === s2); 

examples based on the answers: first , second


In terms of specifications

Upon receiving the property value, in the case of a primitive object, the abstract function ToObject is called . The result called the desired property with the desired this.


In terms of implementation.

The specification does not limit in any way how primitives and objects should be implemented. It also does not regulate the presence / absence of properties of primitives.

For example, for V8

String primitives are sorted into an object of class v8 :: String . Therefore, methods can be called directly on this object.

Strings-objects, in turn, are versed in objects of the class v8 :: StringObject , which extends the class Object and being a full-fledged object serves as a wrapper for v8::String .

This item is a partial translation of the answer to the English question.

    String literals (denoted by double or single quotes) and strings returned by calling String in a non-constructor context (that is, without using the new keyword) are string primitives. JavaScript automatically converts primitives to String objects, so it is possible to use the methods of the String object on string primitives. In contexts, when a method is called on a primitive string or a property is searched, JavaScript automatically wraps the string primitive with an object and calls a method on it or looks for a property in it.

     var s_prim = 'foo'; var s_obj = new String(s_prim); console.log(typeof s_prim); // выведет 'string' console.log(typeof s_obj); // выведет 'object' 

    String primitives and String objects also give different results when using the global function eval() . The primitives passed to eval() are treated as source code; String objects are treated in the same way as all other objects, namely: the object itself is returned. For example:

     var s1 = '2 + 2'; // создаёт строковый примитив var s2 = new String('2 + 2'); // создаёт объект String console.log(eval(s1)); // выведет число 4 console.log(eval(s2)); // выведет строку '2 + 2' 

    Source - https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String