Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript string literals: are they a reference type or a value type? [duplicate]

Tags:

javascript

I know that this question is a bit pointless due to the fact that in Javascript strings are immutable, so in a sense knowing whether they are modeled as a value type or a reference type is not so useful.

So, takes my question just as a curiosity: are string literals in Javascript a value type or a reference type ?

Because I know that there is a lot of confusion with regards to terminology, I would like to clarify what I mean by reference type and value type.

When I say reference type I mean a type for which the values assigned to variables are actually references to objects in memory. This happens with object literals for instance:

const myObject = {foo: "bar"};
const anotherObject = myObject; // both the variables myObject and anotherObject are references to the same memory address. Only one object exists in memory

On the other side when I say value type I mean a type for which the values assigned to variables are the real data representing the intended value of the variable (not a pointer to a memory address containing the real data). This happens with booleans for instance:

const myBoolean = true;
const anotherBoolean = true; // here both of the variables contain the actual data (which is the intended boolean value of the variable). There are two distinct and independent sequences of bytes representing the two distinct and independent boolean data in memory

WHY I DO NOT THINK THIS IS A DUPLICATE

The linked question is somewhat related to mine, but I'm asking specifically about how strings are modeled in memory in a Javascript program, while the linked question asks why it is possible to call methods on string literals and is more related with the idea of boxing explained in its answers.

IMPORTANT NOTE FOR EVERY READER: THIS QUESTION IS ACTUAL POINTLESS DUE TO IMMUTABILITY

See here for a complete explanations.

Here I just report a few quotes from the article linked above:

Since strings are immutable, our original question is moot: there is no way to tell if strings are passed by value or by reference. We can assume that, for efficiency, JavaScript is implemented so that strings are passed by reference, but in actuality it doesn't matter, since it has no practical bearing on the code we write.

And again, at the bottom of the article:

This experiment demonstrates that strings are compared by value. This may be surprising to some programmers. In C, C++, and Java, strings are reference types and are compared by reference. If you want to compare the actual contents of two strings, you must use a special method or function. JavaScript, however, is a higher-level language and recognizes that when you compare strings, you most often want to compare them by value. Thus, despite the fact that, for efficiency, JavaScript strings are (presumably) copied and passed by reference, they are compared by value.

Long story short: the actual implementation probably depends on the particular javascript interpreter (in other words this is an implementation detail). It seems reasonable, for efficiency reasons, to model strings as reference types, but again it probably depends on the actual interpreter used. Anyway, strings immutability makes pointless this whole discussion and this is just a matter for curiosity.

like image 359
Enrico Massone Avatar asked Sep 14 '19 10:09

Enrico Massone


People also ask

Are strings a reference type?

String is a reference type, but it is immutable. It means once we assigned a value, it cannot be changed. If we change a string value, then the compiler creates a new string object in the memory and point a variable to the new memory location.

What are string literals in JavaScript?

Template literals are literals delimited with backtick ( ` ) characters, allowing for multi-line strings, string interpolation with embedded expressions, and special constructs called tagged templates.

Is JavaScript reference or value?

In JavaScript primitive types are passed around as values: meaning that each time a value is assigned, a copy of that value is created. On the other side objects (including plain objects, array, functions, class instances) are references.

Is JavaScript reference type?

Javascript has 5 data types that are passed by value: Boolean , null , undefined , String , and Number . We'll call these primitive types. Javascript has 3 data types that are passed by reference: Array , Function , and Object . These are all technically Objects, so we'll refer to them collectively as Objects.


1 Answers

If you declare strings like

const a = 'a' 
const b = 'a'
a === b // true

then they are value types.

However JS also has a String constructor, so if you do:

const a = new String('a');
const b = new String('a');
a === b // false

In this case they'll be compared by reference. More info

like image 64
Clarity Avatar answered Sep 29 '22 03:09

Clarity