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

“When you introduce a new piece of data into a JavaScript program, the browser saves it as an instance of the data type”

Can somebody please explain this concept in simpler terms? Thank you very much

a = 42

Above we assign an integer value (a number) to the variable, a. When we poll the type of a we are actually polling the type of 42. a is not an object, but a reference to an object. 42 is identified by the interpreter as being a number type so gives it a wrapper object of that type.

typeof 42  =>  'number'

typeof a   =>  'number'

So a refers to an instance of a number type.

31 Likes

I think I understand it better now, thank you for your explanation sir.

4 Likes

Didn’t really understand what is meant by instances, like do they mean literal instaances in time or is describing something else?

1 Like
class Foo {
  
}

foo = new Foo()

Above, foo is an instance of the Foo class. An instance is an object.

9 Likes

An instance is nothing but an object of a class.
class xyx{
xyz obj = new xyz();
}
Here, obj is an instance of class xyz.

Hmmm…

class Xyz {

}
obj = new Xyz();
console.log(obj instanceof Xyz)    //  true
5 Likes

@namibianwolf it’s not really the same kind of instance like you’d assume in physics.
when you learn about classes you’d understand better. for now let me break it down for you:
imagine i give you rice. you know rice is a type of food.
then if i give you beans, you also know beans is a type of food.
so in programming we’ll say: rice and beans are both instances of food!
when we talk about methods and properties, think about it like this:
what could be the properties of the rice i gave you? it might be white!
Also the beans could be brown. Therefore we can say the food class has a “color” property which enabled us to know the color of the rice and the beans.

Now let’s talk about methods. Methods are sort of operations we can carry out on instances of a class. So with the rice and beans from our food class, what operation can we carry out?
We can EAT it! therefore: EAT is a method of food class!

okay stop salivating now, back to coding…hehehe
we have various datatypes, such as strings, numbers, booleans etc
so if i give you 50, it is a number, so it is stored as an instance of number datatype.
if i give you “FC Barcelona”, it is a string, so it is stored as an instance of string datatype.

I hope I was able to help :slightly_smiling_face:

78 Likes

THANK YOU! It’s hard to understand and learn these new things in some of the forums by answering with even more information we’ve yet to come across and understand.

You made this simple. It’s just foreign, so understanding is… much needed. Now for some rice, beans, and tacos. Feel free to add more food references in the future :wink:

When I am finished with the course (and understand to where I can THINK as a coder), I hope to help in the same way.

3 Likes

This is the best explanation for a non-IT background guy like me… :smiley:

2 Likes

Rice or beans, we’re the cook. What escapes learners is control. We are in control. There is no body out there taking it away.

3 Likes

Tell me about Object. I’m not getting what an object actually is…

An object literal is a form of data composed of a block, {}, and containing one or more ‘key: value’ pairs called properties.

object = {
    key: value    // property
}

That’s the literal form. In more general terms, everything in JS is an object (although it may not look like a literal).

 1              =>  'number' type, Number object instance

'a'             =>  'string' type, String object instance

true            =>  'boolean' type, Boolean object instance

function () {}  =>  'function' type, Function object instance

{}              =>  'object' type, Object object instance

[]              =>  'object' type, Array object instance

null            =>  'object' type

For now focus on learning the syntax, how to write an object literal and how to poll the object for the values it contains. More detail will filter in over the duration of the course.

1 Like
  1. You said that 1 is a Number object instance. From what I understood, isn’t 1 an object itself? So wouldn’t 1 be a Number class instance?

  2. So if I understood correctly, a class is like the definition and an instance gives the properties some values. So a class called Person could have many properties such as name, age, height, weight, etc. That is basically the definition of a person. A person has a name, age, height and weight. Then, an instance (E.g. Messi) would give each property a value. It would set its name property to “Lionel Messi”, his age property to 33, his height property to “1.7m” and his weight property to “72Kg”

Class, object, same thing, really.

@mtf

Every string instance has a property called length that stores the number of characters in that string - Codecademy Introduction to JavaScript Lesson 7

"Hello, World!" is a string. However, it is not an instance of the String class. It is a primitive. How come it has the length property? And can you please define the word primitive? I don’t think I totally get it

Primitives are not instances of a class, but they have a wrapper that is given to them by JS based on the recognized type. The wrapper object is what has the length attribute. This is vague, I know. Will need to do some digging to come up with a better explanation.

1 Like

@mtf Will this get explained in further detail later in the JS course?

Not sure there is that great of detail in the course, being more focused on syntax, constructs and basic usage. The technical reading can be rather dry and distracting.

https://blog.mavenhive.in/primitives-vs-objects-in-javascript-1bd9fc0b8fcd

1 Like

Thank you for an article recommendation. Do you think I should read this article now or should I wait until I have a basic understanding of objects? I am currently on properties

1 Like