-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
In JavaScript, properties are the key–value pairs inside an object. The key (property name) is always a string (or symbol), and the value can be any valid JavaScript data type, including numbers, strings, arrays, functions, or even other objects.
Understanding object properties is essential because they allow you to store, retrieve, and manipulate data in objects. Properties can be enumerable, configurable, writable, and can even have getter and setter methods for advanced use cases.
Most properties use strings as keys.
const person = {
firstName: "John",
lastName: "Doe",
age: 25
};
console.log(person.firstName); // John
If a key contains spaces or symbols, you must use quotes and bracket notation.
const employee = {
"full name": "Alice Smith",
"job-title": "Developer"
};
console.log(employee["full name"]); // Alice Smith
console.log(employee["job-title"]); // Developer
Symbols are unique identifiers that can also be used as property names.
const id = Symbol("id");
const user = {
name: "Rahul",
[id]: 101
};
console.log(user[id]); // 101
There are two ways:
Dot Notation – Used when the property name is a valid identifier.
console.log(person.lastName); // Doe
Bracket Notation – Useful when the property name has spaces, special characters, or is dynamic.
const key = "age";
console.log(person[key]); // 25
person.country = "USA";
console.log(person.country); // USA
person.age = 26;
console.log(person.age); // 26
delete person.age;
console.log(person.age); // undefined
Each property in JavaScript has internal attributes:
Writable – Determines if the property value can be changed.
Enumerable – Determines if the property shows up in loops like for...in
.
Configurable – Determines if the property can be deleted or changed to an accessor property.
You can inspect these attributes using Object.getOwnPropertyDescriptor()
:
const obj = { name: "Sam" };
console.log(Object.getOwnPropertyDescriptor(obj, "name"));
/*
{
value: "Sam",
writable: true,
enumerable: true,
configurable: true
}
*/
You can define properties with custom attributes using Object.defineProperty()
:
const person = {};
Object.defineProperty(person, "id", {
value: 101,
writable: false, // Cannot be changed
enumerable: true, // Will appear in loops
configurable: false // Cannot be deleted
});
console.log(person.id); // 101
person.id = 202; // Ignored
console.log(person.id); // 101
JavaScript allows getter and setter methods to control access to a property.
const student = {
firstName: "Rahul",
lastName: "Sharma",
get fullName() {
return this.firstName + " " + this.lastName;
},
set fullName(name) {
const parts = name.split(" ");
this.firstName = parts[0];
this.lastName = parts[1];
}
};
console.log(student.fullName); // Rahul Sharma
student.fullName = "Anil Kumar";
console.log(student.firstName); // Anil
console.log(student.lastName); // Kumar
Getters are useful for computed properties, while setters allow controlled modification.
There are multiple ways to check if a property exists in an object:
in
operator
console.log("firstName" in student); // true
hasOwnProperty()
method
console.log(student.hasOwnProperty("age")); // false
Properties can be listed or iterated using:
for...in
loop
for (let key in student) {
console.log(key + " : " + student[key]);
}
Object.keys()
, Object.values()
, Object.entries()
console.log(Object.keys(student)); // ["firstName", "lastName", "fullName"]
console.log(Object.values(student)); // ["Anil", "Kumar", "Anil Kumar"]
console.log(Object.entries(student));
/*
[
["firstName", "Anil"],
["lastName", "Kumar"],
["fullName", "Anil Kumar"]
]
*/
Note: Getter properties appear in
Object.keys()
andObject.entries()
if enumerable.
Properties are key–value pairs inside objects.
Property names can be strings or symbols; values can be any type.
Properties are accessed using dot or bracket notation.
Properties can be added, modified, deleted, or made read-only using descriptors.
Getter and setter methods allow controlled access to properties.
You can check property existence with in
or hasOwnProperty()
.
Enumerating properties can be done using for...in
, Object.keys()
, Object.values()
, or Object.entries()
.
Understanding object properties deeply helps in working with advanced objects, classes, and APIs efficiently in JavaScript.
Create an object car
with properties brand
, model
, and year
. Then, log the brand
property using dot notation.
Add a new property color
to the car
object and assign it any value. Log the updated object.
Modify an existing property year
in the car
object to a new value. Log the updated property.
Delete the property model
from the car
object. Verify that it has been removed.
Use a property name with space: Create an object employee
with a property "job title"
and assign a value. Access this property using bracket notation.
Check if a property exists in an object: Use in
operator and hasOwnProperty()
to check if the property color
exists in the car
object.
Create a getter property: Create an object student
with firstName
and lastName
. Add a getter property fullName
that returns the full name. Log student.fullName
.
Create a setter property: Using the student
object, add a setter for fullName
that splits a string into firstName
and lastName
. Update the full name using the setter and log both first and last names.
Inspect property attributes: Use Object.getOwnPropertyDescriptor()
to check the attributes (writable, enumerable, configurable) of the car
object's brand
property.
Enumerate all properties of an object: Use for...in
and Object.keys()
on the student
object to list all property names. Compare the output of both methods.
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts