DEV Community

Cover image for Is everything in JavaScript an Object?

Is everything in JavaScript an Object?

mayankav on July 10, 2021

Well well! The motivation for this post comes from my last post where I had a comment from one of my readers that I couldn't agree with. I was talk...
Collapse
 
calinzbaenen profile image
Calin Baenen • Edited

Jokes on you, since everything except numbers and arrays are objects in every language! >:)

(I don't mean Object object, I mean "object" as in a container, like the result of a constructed struct, union, or class.)

(This was only partially relevant.)

Collapse
 
calinzbaenen profile image
Calin Baenen • Edited

To explain how I think about an object, this is what I think of an object as:

type Person struct {
  name [2]string;
  age float32;
} // "Person" type.

p := Person {name: {"Calin", "Baenen"}, age: 14.8333}; // Person object.
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mayankav profile image
mayankav

@baenencalin Didn't get you man! In JavaScript, arrays infact are objects :)

Collapse
 
calinzbaenen profile image
Calin Baenen

To add clarity, strings are a little in the gray area.
If you're using a language like Go, if we disregard the built-in string type, they may be an array, or they may be a struct type, or they may be a wrapper type for an array, which would be an array of numbers, say int16 or int32.

Of course, to be fair, we should consider the built in type - though, how it's built is dependent on the language.
In Java, it's definitely an object, both in the Object sense, and the sense that they are the result of a data structure.

Collapse
 
calinzbaenen profile image
Calin Baenen

What I'm saying in most languages, like C, Go, C++, etc..
Everything except arrays and numbers are objects.

(I guess this isn't necessarily true for JS.)

Thread Thread
 
mayankav profile image
mayankav

@baenencalin Hey man! I get you now. I am not sure about all the other languages but what I can say is that its indeed a choice made by the langauge itself. I mean how a langauge defines primitives and objects is completely upto the language. We do have an article of faith on the universal notion though.

Thread Thread
 
calinzbaenen profile image
Calin Baenen

I thought primitives did have a definite definition ("The least abstract(ed) type in a given language. - Usually that who's implementation is the most bare-bones (compared to other types) when looking at the source code for a given language.")?

Collapse
 
reardon85 profile image
Reardon85

How is a "char" in C an object? And how is a string an object but an array isn't an object?

Collapse
 
calinzbaenen profile image
Calin Baenen

This is an outdated view. I realized not every language is quite like how I described.
Anyway, I never claimed chars were an object (even if they weren't listed as one ov the things that isn't an object); on the note ov arrays and strings, however, I don't consider arrays an object because they're usually just a consecutive arrangement ov data (in C it's literally just a pointer to said arrangement).

I hope this answered your questions thoroughly; feel free to write back if you have any more questions.

Collapse
 
peerreynders profile image
peerreynders

If you assign some property to a string primitive with an intention of getting the value back at some point, you will only be returned undefined because the temporary String Object was discarded then and there.

MDN: primitive values:

All types except objects define immutable values (that is, values which can't be changed).

seems like a more straightforward explanation - i.e. nothing was changed in the first place - JavaScript just ignores the attempt of mutation.

Collapse
 
mayankav profile image
mayankav • Edited

@peerreynders Indeed. Thanks for taking out time to read :)

Collapse
 
lilithkarapetyan profile image
Lilit Karapetyan

Thank you for bringing up this topic. I heard that phrase so much that it was confusing me at the beginning of my career.
I think this myth was created by the people working in other languages who started to get acquainted with JS just a little bit :)
I don't think it is even possible for the language to only have objects as its type (Correct me if I'm wrong).
The most important note here was about the optimization part. Please, please, do not try to do these kinds of micro-optimizations yourself :) The browser engines are designed to detect and optimize all the "normal" code flows. If you try to do some weird things, the browser engine will not detect recognizable patterns and will fail to optimize the performance.

Collapse
 
mayankav profile image
mayankav

@lilithkarapetyan Hello Lilit! To answer your question, I'd say it completely depends on how a particular language defines primitives and objects. In JS, objects are nothing more than collective key:value pairs and anything that is not an object is a primitive. Consider another language, maybe 'C', it maps primitive types much closer to the underlying microprocessor architecture (int, char etc..). Generally in computer science "primitives" emply atomic types but then every programming language can freely decide on how to implement their own basic building block. For example Python chose to keep objects at the atomic level. So yeah, it depends on the language.

Collapse
 
frontwebdev profile image
frontwebdev • Edited

. It is important not to confuse a primitive itself with a variable assigned a primitive value. The variable may be reassigned to a new value, but the existing value can not be changed in the ways that objects, arrays, and functions can be altered. (from mdn)

and you confused it.
when you write let str = 'hello' str is not primitive.
now try Object.isExtensible(str) you will get false and that a reason why you cannot add new prop to variable str.

Collapse
 
ahmedgmurtaza profile image
Ahmed Murtaza

Well explained!

Collapse
 
mayankav profile image
mayankav

:)