What does it mean by an instance of a data type?

You’re welcome. It’s a brief article, and not very dense. Five minute read.

1 Like

Great! I’ll check it out tomorrow. I spent nearly 3 - 5 hours on properties :tired_face:

Some more reading from the MDN documentation…

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

1 Like

hi @samyadel
The number 1 is a Number object instance. If you recall, in the lesson on datatypes, the building block or primitive data type that is a collection of related data is the object datatype .
A class is just a template for creating objects. For example

class Player {
constructor(name,goals){
this._name = name
this._goals = goals
}
}
const messi = new Player(“Lionel Messi”,25)
const suarez = new Player(“Luis Suarez”,16)

The above class and instances would be the same as if we just created an object for both messi and suarez and set the name and goals property like so…
const messi {name : “Lionel Messi”, goals: 25}…

So we can see clearly that the object datatype is the basic datatype for a collection of data and classes just like factory functions are templates for creating objects.So basically Java Script tries classifies everything you type as an instance of one of the 7 datatypes, if it can’t be classified an error is produced. EG typing a string like : Sergi Roberto without the quotation marks will produce an error.
Hope this helps.

5 Likes

@ruronite
Thanks for your explanation! Just to correct you, in JavaScript, a value can either be a primitive or an object. The string "Hello, World!" is not an instance of the String class

console.log("Hello, World!" instanceof String); // false

However, each data type has a corresponding class. So you could create a string like the following: new String("Hello, World!"). However, "Hello, World!" is not equal to new String("Hello, World!")

console.log("Hello, World!" === new String("Hello, World!"));

"Hello, World!" is a primitive while new String("Hello, World"); is an object and an instance of the String class

console.log(typeof "Hello, World!"); // "string"
console.log(typeof new String("Hello, World!")); // "object"

console.log(new String("Hello, World!" instanceof String)); // true

1 Like

@samyadel
I see what you’re trying to say,but you might have misunderstood me.

I said the Number 1 is an instance of the number object and not the number class. Then I proceeded to state how classes are slightly different from object. The topic here is instance of a datatype not instance of classes.To check instances of datatypes, you use

console.log(typeof 42);
// expected output: “number”

console.log(typeof ‘blubber’);
// expected output: “string”

console.log(typeof true);
// expected output: “boolean”

console.log(typeof undeclaredVariable);
// expected output: “undefined”

This example above is actually the direction i was rowing in. That what ever you type in javascript is classified as an instance of one of the 7 datatypes.

2 Likes

Ah ok! I thought you meant the number 1 is an instance of the Number class

1 Like

Technically, 42 is primitive and not object in Javascript right?

Right. JS transforms it to an object by giving it a wrapper of type ‘number’.

What does to poll mean? thanks.

So “instance of” kind of means the same thing as “example of”?

Consider,

a = 42
print (a)    #  42

Above we wanted to print the value represented by the variable a. To do that we poll the variable.

@allisge saying “example of” may help you understand the concept, but what I want you to keep in mind is that it goes beyond that. When we say “allisge” is an instance of the string data type, It means the properties and methods associated with the string object such as .length and .split() can be applied on “allisge”.

If you tried applying properties and methods associated to one data type on another data type, you’ll get a TypeError or undefined .
eg

let a = 2020;
console.log( a.split(" ") )
//returns TypeError

but ,

let a = “allisge would be a great programmer”;

console.log(a.split(" "))
[ ‘allisge’, ‘would’, ‘be’, ‘a’, ‘great’, ‘programmer’ ]

Above we tried to apply .split() method on an instance of a Number datatype and an instance of the string data type.
However .split() is a method of the string object and not the number object, hence it works on the instance of a string data type but not on the instance of the number data type.

7 Likes

This was such a great analogy thank you so much!

1 Like

It’s like saying JS necessarily understands every bit of data as one of the possible data types it can work with. And that recognition comes with a specific set of properties, depending on the type of data, that can be called or used by that instance.

Thank you very much Sir

1 Like

Thank you @ruronite for this simple but efective explanation :slight_smile:

1 Like

This is outright genius! I mean, feel free any time to use food to explain code :grin:.

1 Like

This is the best explanation I’ve seen anywhere on this forum for anything! Analogies in real-life terms are so helpful. THank you

1 Like

I know its almost a year later, but I just wanted to say this helped me tremendously. Thank you so much!

1 Like