Free Ebook cover How to use Javascript in website development

How to use Javascript in website development

5

(2)

37 pages

Objects in Javascript

Capítulo 7

Estimated reading time: 2 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

Objects are fundamental in Javascript, as they allow the creation of complex structures to store and manipulate data. In Javascript, everything is an object, from a simple string to a function.

An object is a collection of properties, which are key-value pairs. The key is a string that identifies the property, and the value can be any data type, including another object. For example:


const person = {
  name: "John",
  age: 30,
  address: {
    street: "Street A",
    number: 123
  }
};

In this example, we have an object called "person" with three properties: "name", "age" and "address". The "address" property is, in turn, another object with two properties: "street" and "number".

To access a property of an object, just use the dot notation or square brackets. For example:


console.log(person.name); // "John"
console.log(person["age"]); // 30
console.log(person.address.street); // "Street A"

It is also possible to add or modify properties of an object dynamically:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app


person.email = "joao@gmail.com";
person.age = 31;

In addition, it is possible to create objects from constructor functions or classes. For example:


function Person(name, age) {
  this.name = name;
  this.idade = age;
}

const person1 = new Person("Mary", 25);
const person2 = new Person("Peter", 35);

In this example, the "Person" constructor function defines an object with two properties: "name" and "age". By creating a new object from this function with the "new" operator, we can pass the desired values ​​to the properties.

In summary, objects are a fundamental part of JavaScript and allow the creation of complex structures to store and manipulate data. With dot or square bracket notation, you can access and modify the properties of an object, and you can also create objects from constructor functions or classes.

Now answer the exercise about the content:

_What is the definition of an object in Javascript?

You are right! Congratulations, now go to the next page

You missed! Try again.

An object in JavaScript is a collection of properties, which are key-value pairs. The keys are strings that identify the properties, and the values can be any data type, including other objects.

Next chapter

DOM Manipulation with Javascript

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.