0

I've been doing a lot of research on this topic for over 2 weeks now and I'm only asking the question because MDN's documentation on mutability says only arrays and objects are not primitive values and of course that's true, they have reference values.

However, I thought functions, string object, Date, and RegEx also had reference values because they are also non-primitives. For example if we use slice() on a string primitive, javascript will automatically convert(reassign) it to a string object and now it's mutable as the word 'snow' becomes 'know' through reassignment. This won't work with const because it disables reassignment.

let word = 'snow'
console.log(word) // "snow"
word = `k${word.slice(1)}`
console.log(word) // "know"

So I'm guessing either MDN is wrong or a lot of other resources like dotnettricks and even a few javascript certifications guides are wrong...

2 Answers 2

2

MDN isn't wrong - functions, Date and RegEx are objects.

Also, string is still primitive.

let a = 'snow';
let b = a;
a = 'know';
console.log(b); // snow
Sign up to request clarification or add additional context in comments.

8 Comments

functions are callable objects.
They're still objects.
In this example, b isn't a "reference" to a, but rather assigned the current value a? When a is reassigned, the value doesn't update because it's stored by the value, not by reference? I guess what I'm confused about then is how the string object wrapper works and references. A string is primitive but it has a string object wrapper. Slice isn't available on the primitive so, at runtime, the language uses the wrapper object and swaps the letter? But let's say we use the sting object directly: ``` const str = new String('The quick dog.') str.slice(-5) // dog. console.log(str) ```
b isn't a reference to a, because in JavaScript you can't have references to strings. Also, your code logs "The quick dog.". It's not going to work, string is in all ways a primitive.
Date is definitely by-reference (try setFullYear), RegExp also is by-reference (you can check that by setting its lastIndex property and checking the other reference). Functions should be by reference though I can't find a property I can write to.
|
1

"strings" is primitive and new String("strings") is an object but in both cases the sequence of characters is immutable.

in your example you didn't really mutate the string you just create a new string and assign it to the same variable

var primitiveString = 'hello';
var objectString = new String('hello');
var objectStringRef = objectString;

// string are primitives so you can not add properties to it
primitiveString.newProperty = "my property";

console.log(primitiveString.newProperty) // undefined

// You can add properties to String objects
objectString.newProperty = "my property";

console.log(objectString.newProperty) // "my property"

// and you can have a reference to a String object
console.log(objectStringRef.newProperty) // "my property"

// but both are immutable
primitiveString[0] = "p";
objectString[0] = "p";

console.log(primitiveString) // "hello"
console.log(objectStringRef.toString()) // "hello"

2 Comments

yeah, exactly. Well, it still mutates but the mutation only happens through reassignment which is why const doesn't work, it disables reassignment and throws a type error. The more weird part is on the browser when you inspect a String object it says primative type regardless of the literal '' or new String(). Also String instanceof Object // true and Array instanceof Object // true yet one is a reference and the other isn't. A bit confusing until I understood it as just constructors != reference value.
@HansDev to be a mutation the object should be the same after the change but that is not the case. it is just a plain reassignment. using const instead of let will probably demonstrate it more easily. you can assign an array to a constant and change its content (mutate) const A = [1, 2, 3]; A.pop(); console.log(A)//[1, 2]; there is no way you can do the same to a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.