Lorenzo Zabot
Lorenzo Zabot

Reputation: 327

Data types, values and objects

I'm getting pretty confused about this data type thing, especially with objects.

I read that a data type is basically an attribute for information/data used in our programs that tell the interpreter what operations can be done with that information without causing errors. Hence, every piece of information has an associated data type.

JS has 7 primitive data types and the data type Object.

Now, suppose that I write:

let person = {
   name: "John",
   age: 39,
};

Every article I found on the web says that person is a variable that store an object.

That's what gets me confused.What I'm asking is, wouldn't be more correct to say that "person is a variable that refers to a value whose data type is Object"?

I'm sure this is just overthinking the topic, however I want to get the correct basics of programming before moving on.

Upvotes: 0

Views: 270

Answers (1)

Salketer
Salketer

Reputation: 15711

I think you are way overthinking it for now... Without much talking about typing, because there's much more to it than just Object. Also know that in Javascript, every value is an object, or almost.

What's good to figure out though, as you mentioned: person is a variable that refers to a value.

This is important, that makes all this more obvious:

let person = {name:"John",age:39};
let john = person;
john.age++; // Increase john's age by 1.
console.log(person.age); // 40

As you can see, person and john both refer to the same object. Untill I put another reference on them:

let person = {name:"John",age:39};
let john = person;
john.age++; // Increase john's age by 1.
console.log(person.age); // 40
person = {name:"Mary",age:34};
console.log(john.name) // John

Here, we replace the reference on person. john remains the same. Now both variable have "diverged" from each other.

Upvotes: 2

Related Questions