JavaScript and String as a primitive value

In JavaScript, a string is a primitive value. But it is also a String object ... The initial value is the value entered directly into the variable.

So my question is:

var d = "foo"; 

contains d directly foo or a link to a string object, for example, to other languages?

Thanks.

+4
source share
6 answers

If I understand correctly, d will contain the string literal "foo", not an object reference. However, the JavaScript engine will effectively apply the literal to the String instance when necessary, so you can call the String.prototype methods in string literals:

 "some string".toUpperCase(); //Method of String.prototype 

The following snippet from MDN can help explain it further (highlighted by me):

String literals (indicated by double or single quotes) and strings are returned from String calls in the context of a non-constructor (that is, without using a new keyword) - these are primitive strings . JavaScript automatically converts primitives and String objects so that you can use String Methods for primitive strings. In the context where the method is to be called in a primitive string or property search, JavaScript will automatically wrap the string primitive and call the method or perform a property search.

All of this is explained in detail in the specification , but it is not quite a simple read. I recently asked a question (about why this can be done above), so it might be worth reading a (very) detailed answer.

+6
source

if you define

 var d = "foo"; 

than d contains foo directly but if you define

 var S = new String("foo"); 

then S is Object

Example:

 var s1 = "1"; var s2 = "1"; s1 == s2 -> true var S1 = new String("2"); var S2 = new String("2"); S1 == S2 -> false 
+3
source

I think every variable in Javascript actually represents an object. Even a function is an object.

+2
source

I found two useful articles detailing this: here and here . It seems that the primitive types in JavaScript are passed VALUE (i.e. when you pass, if the function gets "isolated" in the function and the original value of the variable does not change), while the reference types are passed, you guessed it, using LINKS and passing it to a function will change the original variable.

The primitive types in JavaScript are text (string), numeric (float / int), boolean, and NULL (and the scary type is undefined). Any user-defined objects, functions, or standard arrays are considered reference types. I have not explored the Date type yet, but I'm sure it will fall into primitive types.

+1
source

Found this page about javascript variables, it seems that:

Primitive type for javascript: booleans, numbers and text.

0
source

I believe that in Javascript there are no primitives, in the sense of Java, at least - all this is an object of some kind. So yes, this is an object reference - if you extend the String object, d will have this extension.

If you mean primitives, as in the types provided by the language, you have several logical numbers, numbers, strings and dates that are all determined by the language.

0
source

Source: https://habr.com/ru/post/1396576/


All Articles