Javascript, what happens when we assign value to read-only data property such as string length?

Suppose we have a string, length is read-only data property of string. What happen if we unintentionally assign value to length property of string? Why javaScript return those value as a result?

for example if we run this part of code:

var str="test";

output should be like this:


the third line of code returns 3, My question is why assignment in javaScript returns right side of operand not left side of it?


The assignment operator always returns the right-hand value. Hence, passing the expression inside the console.log function returns the right-hand value, in your case 3.

Assigning any value to the length property of a string has no effect.

let myString = "Hello,World!";

myString.length = 4;

// expected output: "Hello,World!"

// expected output: 12