Introduction to Object Properties in JavaScript

js object properties
10 November 2024


JavaScript is one of the popular programming languages used for developing websites and various applications. In this language, objects are a key concept that allows you to create more complex and organized data structures. This record helps you write more efficient and maintainable code.


In JavaScript, objects can include properties and methods. Properties are characteristics assigned to an object, and they define its attributes. For example, an object can represent a user with properties such as name, age, and email.


To access a property of an object, you can use the object name and apply . (dot) or [] (bracket) notation. These methods allow you to conveniently retrieve or modify data.


Here’s a simple example of defining and using the properties of an object in JavaScript:


Example and Explanations



let user = {\r\n name: "Ali",\r\n age: 30,\r\n email: "[email protected]"\r\n};\r\n\r\n// Accessing object properties\r\nconsole.log(user.name); // Ali\r\nconsole.log(user["age"]); // 30\r\n\r\n// Changing a property\r\nuser.email = "[email protected]";\r\nconsole.log(user.email); // [email protected]\r\n

Line by Line Explanation


let user: An object named user is defined.

name: "Ali": The property name is assigned the value "Ali".

age: 30: The property age is assigned the value 30.

email: "[email protected]": The property email is assigned the value "[email protected]".

console.log(user.name): Outputs the value of the name property from the user object.

console.log(user["age"]): Outputs the value of the age property from the user object using bracket notation.

user.email = "[email protected]": Changes the value of the email property to "[email protected]".

console.log(user.email): Outputs the new value of the email property.


FAQ

?

How can I access a property of an object?

?

How to change a property in an object?

?

Can I add a new property to an object?