DEV Community

Cover image for Weird behaviors of javascript: Primitive Types and Reference Types

Weird behaviors of javascript: Primitive Types and Reference Types

MutluDev on August 19, 2020

I recently learned a difference between Primitive types and Reference types. I thought it would be great to write a blog post about this topic. ...
Collapse
 
alexanderjanke profile image
Alex Janke

Animation

Collapse
 
pentacular profile image
pentacular

There is no pass by reference in this article.

Collapse
 
alexanderjanke profile image
Alex Janke

Not pass by (no functions in this article) but it's basically the same principle for types and assigning objects to new variables- still just a reference

Thread Thread
 
pentacular profile image
pentacular • Edited

Not in the least.

let a = { v: 1 };
const foo = (p) => { p = { v: 2 }; };
foo(a);
console.log(a);

What does this print?

Which of your images does it reflect?

Thread Thread
 
alexanderjanke profile image
Alex Janke • Edited

What does this print?

An error. That is syntactically wrong

Thread Thread
 
pentacular profile image
pentacular

Try now ;)

Collapse
 
shane profile image
Shane McGowan

Nice write up! I wouldn't call these behaviors weird though, this is by far the least weird behavior of JavaScript

Collapse
 
mutludev profile image
MutluDev

Thanks, you're right this is not that weird, i wanted to make this as a series, i don't know what i will post next but i definitely consider "why 0.1 + 0.2 = 0.30000000000000004" which is lot weirder than this

Collapse
 
nombrekeff profile image
Keff • Edited

It is weird. I'd love to know more about this!

It's not that weird actually. It's just how computers handle floating numbers.

Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits. In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits.

Excerpt from this post: docs.oracle.com/cd/E19957-01/806-3...

Really interesting read, I had not read much about this whole topic!

Thread Thread
 
mutludev profile image
MutluDev

Yeah, it feels weird when you see it first, after you've learned how numbers work in JavaScript, everything makes sense.

youtube.com/watch?v=MqHDDtVYJRI
This video really helped me to grasp how numbers work in JavaScript,.

Excerpt from this post: docs.oracle.com/cd/E19957-01/806-3...

This looks really interesting I will read this, thanks.

Collapse
 
pentacular profile image
pentacular

Could you point out where 'reference types' are mentioned in the ecmascript spec?

I can't find it anywhere in the language specification.

Collapse
 
mutludev profile image
MutluDev

es5.github.io/#x8.7 this might help

Collapse
 
pentacular profile image
pentacular

It would if it talked about object references.

The references mentioned there are property references.

For example, in delete a.b; a.b is a reference.

"The Reference type is used to explain the behaviour of such operators as delete, typeof, and the assignment operators."

Can you find anything about 'reference types' that's relevant to object values or anything mentioned in this article?

To be specific, anywhere that supports the claim that objects, functions, and arrays are reference type values.

Collapse
 
selahattinunlu profile image
Selahattin Ünlü

Hi, I realized that you're looking for a job! We're hiring. But I could not see any information to contact with you. Can you please share an information to contact with you? :)

Collapse
 
halildndar profile image
Halil Dündar

Very helpful and informative article, thank you for posting that.

Collapse
 
mutludev profile image
MutluDev

Thank you for your polite comment.