what is property in javascript

What is Property in JavaScript?

In JavaScript, property refers to the characteristics or attributes of an object. It can be a value associated with the object or a function that can be executed on the object. Properties define the behavior of an object in JavaScript programming.

Properties are used to store and retrieve data from objects. They can be accessed using dot notation or square brackets notation. Dot notation is used when the property name is known and valid in JavaScript syntax, while square bracket notation is used when the property name is not known or contains invalid characters in JavaScript syntax.

Creating Properties

Properties can be created using two methods:

  • Literal Notation: It involves creating an object and its properties at the same time. Here's an example:
const person = {
  name: 'John',
  age: 30,
  hobbies: ['reading', 'cooking']
}
  • Constructor Notation: It involves creating an object and then adding properties to it. Here's an example:
const person = new Object();
person.name = 'John';
person.age = 30;
person.hobbies = ['reading', 'cooking'];

Accessing Properties

Properties can be accessed using dot notation or square bracket notation. Here's an example:

console.log(person.name); // Output: John
console.log(person['age']); // Output: 30

Deleting Properties

Properties can be deleted using the delete operator. Here's an example:

delete person.hobbies;
console.log(person); // Output: {name: 'John', age: 30}

Overall, properties are a crucial aspect of object-oriented programming in JavaScript. They define the behavior of objects and allow us to manipulate and work with data in a structured way.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe