JavaScript object property is a key-value pair that defines a specific attribute or characteristic of an object. Properties can be thought of as variables that belong to an object, and are used to store and retrieve data related to the object.
Here is an example of an object with three properties:
const person = {
name: 'John',
age: 30,
isStudent: false
};
The Object properties are defined using a syntax that consists of a property name and its corresponding value, separated by a colon (:). Multiple properties are separated by commas (,).
JavaScript object property example
Simple example code access object properties using either dot notation or bracket notation.
<!DOCTYPE html>
<html>
<body>
<script>
let person = {
name: 'John',
age: 30,
isStudent: false
};
console.log(person)
// using dot notation
console.log(person.name);
// using bracket notation
console.log(person['age']);
</script>
</body>
</html>
Output:
You can also add, delete, and modify object properties using the following code:
// adding a new property
person.gender = 'male';
// deleting a property
delete person.isStudent;
// modifying a property
person.age = 35;
Comment if you have any doubts or suggestions on this Js object topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version