What is an object?

Ive learned on this website and on MDN that an object is something with properties and methods but mostly everything in JavaScript is can be treated as an object so it is hard to tell what an object is. Some articles say that objects are just data containers others say that objects are neat and tidy packages of information about something you want to use in your code. so are the only objects arrays, object literals, and functions. Are these the only objects in JS?



The key thing to understand is the different between objects and primitive types. Primitive types, such as numbers, strings and Booleans (true / false) are stored directly in memory. An object, on the other hand, is just a collection of primitive types and is stored as a reference to them.

When a primitive value is copied, the value is added to another location in memory. However, when you copy an object, the collection of data that it represents does not change. We simply create another reference to the same underlying values. The difference between passing by reference and passing by value is one of the most important concepts in JavaScript.

When you change a value inside an object, the object reference itself does not change. This is why objects can be declared with the const keyword and still mutated. The object is constant, even though its data changes.

1 Like

Javascript never passes anything by reference. It has object references, which it passes by value. Here’s some explanation of this for Java, and the story is exactly the same for JavaScript.

Of course, everyone does confuse this terminology, so maybe I’m tilting at windmills here.
Getting the term right does become important however when you start working with languages like C++ and C#, which support pass-by-value and pass-by-reference semantics.

To talk about Objects, you need to first start with Classes.

A Class is a data structure that contains/combines state properties (or fields) and actions (methods, functions).

If you instantiate or create an instance of that Class, the result is an Object – which is basically just an instance of that Class.

Let’s say we create a Class called Point for example, that stores 2 numbers. Let’s call the 2 numbers stored as X, Y. And they represent a point on the screen, x axis, y axis.

The class would look like this: C#

public class Point
    public int x, y;
    public Point(int x, int y) 
        this.x = x;
        this.y = y;

Then later on in your program, you can create 2 or however many variables you want that all derive from the same Point Class.

Point p1 = new Point(0, 0);
Point p2 = new Point(10, 20);

In this case, p1 and p2 are the objects.

I don’t think bringing classes into the picture here is helpful

In a sense an object is just a dictionary with string keys and arbitrary values, and a lot of JS outside of primitives such as floats are just augmentations of those

Functions for example are just objects too, with string keys, but they possess special syntax only available to them for calling

Similarly the objects that come from JS classes are plain objects (i.e. a dict) with special significance associated with their prototype member

I think it’d be very misleading to bring up classical OO here also when JS follows prototypal OO instead

I think OP’s question was really asking about what a phrase like “In Javascript, everything is an object” could mean

1 Like