Objects in JavaScript are the main elements of this programming language. Just like objects in real life, These are made up of a variety of properties that characterize them. In this post, we will teach you what objects are in JavaScript and how to identify them exactly when developing a project.
What are objects in JavaScript?
In our process of learning how to develop with JavaScript, The first obstacle or great element that we must understand is the functions, elements that allow us to create a diversity of actions. If you want to learn more about them, we invite you to read our posts on how to write functions, how to write arrow functions and how to execute a function in JavaScript. This element is also complemented by the use of callbackswhich refers to using one function as an argument to another.
The second major element that We must understand to master this programming language are the objects in JavaScript. In JavaScript, every variable we write in our code ends up being an object. This happens because, in this way, the element that is not an object ends up behaving like one.
How are objects characterized in JavaScript?
In JavaScript, every object we have has a property prototype which points to its prototype. That is, if we have a string, that string behaves like an object whose prototype is a string. In turn, this prototype points to another property prototype which points to another prototype. And what happens next? Well, this prototype points to another, and this one to another.
This happens successively until we reach the object object, What is the parent prototype?. The object objects is the end of this prototype chain, because its prototype is null.
To illustrate this situation, we propose an example. Suppose we have in our JavaScript editor a string worth Hello like the following:
let myString = ‘Hello’
So if we insert the variable myString in our editor, it will return ‘Hello’. In addition to this, We can use the variable followed by a period to ask for a variety of properties of our string. We can, for example, write myString.length to ask its length.
We can also ask the index of the letter and in our string using the command myString.indexOf(‘e’) and our program will return the number 1 as a response. Keep in mind that index positions in JavaScript always start at the number 0so, if we ask the position of the letter either with the command myString.indexOf(‘o’), the program will respond ‘4’.
The command variable.property It is the way to access the properties of an object. Thus, the fact that our string ‘Hello’ contains different properties that we can access, it means that this string Its an object. This is what we were referring to at the beginning of the article when we said that all elements in JavaScript are objects, since they behave as such.
The fact that elements like string behave like objects, it allows us to do various actions on them, since An object also has what we know in JavaScript as methods.
When we talk about methods we are referring to functions that are properties of an object and there are many for each object. For example, one of the methods we could use with our string is the method of converting to uppercase. To access this, we use the same command as with a property, as you can see in the following line:
myString.toUpperCase
The result of this method will be the following:
‘HELLO’
The existence of methods within our string confirms that variables in JavaScript end up being objects. Furthermore, in the same way that we access its properties, we can access the prototype of our string with the following command:
myString._proto_
This command will return the result String. When we display the arrow next to this result, we will see a bunch of methods and we will find that, in the characteristic of [Prototype], the value is object. Within this object, we will find another _proto_ which tells us that it is an object, under which there is another characteristic _proto_ which has as value null. This feature tree is what we know as the prototype chain.
Do you want to continue learning?
Now that you know what objects are in JavaScript and what their characteristics are, We are sure that you want to know more about the mysteries and peculiarities of this programming language for the web. Therefore, we invite you to continue learning with us in our Full Stack Web Development Bootcamp.
In this comprehensive and intensive training, you will not only learn how to master JavaScript to create and develop your own projects, but you will also learn to master other programming languages, such as HTML. What are you waiting for to join and fulfill your dream of being a web developer? Don’t hesitate and sign up to succeed in the IT sector!