This article gives a demonstrated overview of the working and usage of the object properties in JavaScript.
This article serves the following outcomes:
What are the Object Properties in JavaScript?
In JavaScript, properties point out the set of values that are associated with the object. JavaScript has the ability to add, delete, and modify an object’s properties with different methods. In simple words, property represents the characteristics of an object. Users can access the property by utilizing the dot operator.
For better understanding by the user, the syntax of object properties is given here.
Syntax
The simple syntax to access the property of the object is given below:
or
or
The description of the parameters is listed below:
- objName: Denotes the name of the object.
- propName: Represents the name of the property in JavaScript.
Example 1: Display the Different Properties of an Object
An example is demonstrated to assign the object properties and display the names of properties.
let teacher = {};
teacher.name = 'John';
teacher.id = '052-39';
teacher.age = 25;
teacher.color = 'black';
for (let property in teacher) {
console.log(property);
}
In the code:
- An empty object named “teacher” is declared.
- Values are assigned to the object using the dot operator.
- Lastly, for loop and the console.log method are utilized to print the names of properties.
Output
The display shows the name, id, age, and color properties of the teacher object in JavaScript.
Example 2: Modify the Properties of an Object in JavaScript
Another example is provided to perform modification of the properties of an object in JavaScript.
const teacher = {
name: 'Harry',
age: 30,
subject: 'English',
performance: 'Grade B',
}
console.log(teacher)
// Modify the property of an object
deleteteacher.performance
teacher.performance = 'Grade D'
teacher.subject = 'Math'
console.log(teacher)
In this example, the description is as follows:
- First, assign a specific value to different properties of an object.
- After that, the delete keyword is utilized to delete the value of the teacher.performance property.
- In the end, display all the properties and values of the teacher object after modification of the values.
Output
The output shows the old properties as well as the modified set of properties.
Example 3: Delete Object Properties
Like modifying, JavaScript allows you to delete a property of an object. To do so, a delete operator is employed.
name: 'LinuxHint',
type: “Tech”
}
console.log(delete web.type);
In the above code, an object named “web” is created that has two key values. Using the delete operator, the type property of the “web” object is deleted.
The output has returned true, which states that the type property has been deleted.
Conclusion
In JavaScript, object properties are said to be the set of values that are associated with the object. Arrays, objects, dates, and functions are always objects. However, Booleans, Strings, or Numbers can also be objects. You have learned a brief overview of the object properties in JavaScript, and a set of examples is also provided that demonstrate how to play with object properties in JavaScript.