Mark As Completed Discussion

Objects

In JavaScript, objects are a fundamental data type that allows you to store and manipulate data in key-value pairs. Objects in JavaScript are similar to real-life objects, which have properties and behaviors.

Creating Objects

You can create an object in JavaScript using object literal syntax. Here's an example:

JAVASCRIPT
1const person = {
2  name: 'John Doe',
3  age: 25,
4  city: 'New York'
5};

In this example, we have created an object person with three properties: name, age, and city. The properties are assigned values using the colon :.

Accessing Object Properties

You can access object properties using dot notation . or bracket notation []. Here's how you can access the name property of the person object:

JAVASCRIPT
1console.log(person.name); // Output: 'John Doe'

Modifying Object Properties

You can modify object properties by assigning a new value to them. Here's an example:

JAVASCRIPT
1person.age = 30;
2console.log(person.age); // Output: 30

Object Methods

In addition to properties, objects can also have methods, which are functions associated with the object. Here's an example:

JAVASCRIPT
1const car = {
2  brand: 'Tesla',
3  model: 'Model S',
4  start: function() {
5    console.log('The car is starting...');
6  }
7};
8
9car.start(); // Output: 'The car is starting...'

In this example, the car object has a method start that can be called using dot notation car.start().

Objects are powerful and versatile in JavaScript. They allow you to organize and manipulate data in a structured way, making them a crucial part of web development.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment