JS Tutorial



JS OBJECT PROPERTIES


JavaScript Object Properties

In JavaScript, an object is a standalone entity, with properties and methods. An object property is a key-value pair, where the key is a string (also called a property name) and the value can be any data type, such as strings, numbers, arrays, and even functions.

πŸ”Ή Key Concepts of Object Properties:
  • Property Name (Key) – The name of the property (also called the key) of an object
  • Property Value – The value associated with a property, which can be any valid JavaScript data type (string, number, array, function, etc.)
  • Dot Notation – A way of accessing object properties using a dot (e.g., object.property)
  • Bracket Notation – A way of accessing object properties using square brackets (e.g., object['property'])

πŸ“Œ Defining Object Properties

In JavaScript, we can define an object using the following syntax:

Syntax:

const objectName = {
  propertyName1: value1,
  propertyName2: value2,
  // more properties
};

Here, objectName is the object, and propertyName1, propertyName2, etc., are its properties. The values associated with each property can be of any type.

πŸ“Œ Accessing Object Properties

You can access an object’s properties using two main techniques:

1. Dot Notation

Dot notation is the most common way to access a property of an object.

Syntax:

object.propertyName;

Example:

const person = { name: "Alice", age: 25 };
console.log(person.name); // Output: Alice

2. Bracket Notation

Bracket notation allows you to access object properties with a string key, and it’s useful when the property name contains special characters or spaces.

Syntax:

object["propertyName"];

Example:

const person = { name: "Alice", age: 25 };
console.log(person["name"]); // Output: Alice

πŸ“Œ Modifying Object Properties

You can modify the properties of an object by simply reassigning a new value to an existing property.

Syntax:

object.propertyName = newValue;

Example:

const person = { name: "Alice", age: 25 };
person.age = 26;
console.log(person.age); // Output: 26

πŸ“Œ Deleting Object Properties

To remove a property from an object, you can use the delete operator.

Syntax:

delete object.propertyName;

Example:

const person = { name: "Alice", age: 25 };
delete person.age;
console.log(person.age); // Output: undefined

πŸ“Œ Adding New Properties to an Object

You can easily add new properties to an object using dot notation or bracket notation.

Syntax:

object.newProperty = value; // Using dot notation
object["newProperty"] = value; // Using bracket notation

Example:

const person = { name: "Alice", age: 25 };
person.city = "New York";
console.log(person.city); // Output: New York

πŸ“Œ Summary of JavaScript Object Property Operations:

  • Objects in JavaScript are collections of key-value pairs, where the key is a string (property name) and the value can be any data type.
  • You can access properties using dot notation or bracket notation.
  • Properties can be modified, deleted, or added using different techniques.
  • JavaScript objects are dynamic, meaning you can change their properties during runtime.

πŸ“Œ Practice Exercise: Modify the Object Properties

Try modifying the object below by changing its properties:


Your result will appear here

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review