JavaScript Objects

What is an Object?

An object is a collection of related data and functionalities, organized as properties and methods. Properties are values associated with an object, while methods are functions that perform actions on the object’s properties.

Creating an Object

You can create an object using object literal syntax, which involves defining the object within curly braces {} with properties as key-value pairs.

Tutorials dojo strip

Explanation of Code:

The person object has properties firstName, lastName, age, and isEmployed with their respective values.

JavaScript

Accessing Object Properties

You can access the properties of an object using dot notation (object.property) or bracket notation (object["property"]).

Explanation of Code:

The firstName property is accessed using dot notation, and the lastName property is accessed using bracket notation.

JavaScript

Adding and Modifying Object Properties

You can add new properties to an object or modify existing ones using dot notation or bracket notation.

Explanation of Code:

A new property middleName is added, and the age property is modified.

JavaScript

Deleting Object Properties

You can delete properties from an object using the delete operator.

Explanation of Code:

The isEmployed property is deleted from the person object.

JavaScript

Methods

Methods are functions that are stored as properties of an object.

Explanation of Code:

The fullName method concatenates the firstName and lastName properties to return the full name of the person.

JavaScript

JavaScript Objects Example Code

HTML

JavaScript Labs

Tutorials dojo strip