JavaScript Object Properties
Last Updated :
20 Nov, 2024
JavaScript objects are collections of properties, where each property is defined as a key-value pair. Understanding object properties is important to working with JavaScript as they enable dynamic storage, access, and manipulation of data.
Defining Object Properties
You can create objects using object literals, defining properties as key-value pairs.
JavaScript
let obj = {
name: 'Sourav',
age: 23,
isActive: true
};
console.log(obj.name);
Here, name, age, and isActive are properties of the object. The values can be of any type, including strings, numbers, and booleans.
Accessing Object Properties
Properties can be accessed using dot notation or bracket notation.
JavaScript
let obj = {
name: 'Sourav',
age: 23,
isActive: true
};
console.log(obj.age); //dot notation
console.log(obj['isActive']); //bracket notation
- Dot Notation: Simple and easy to iuse, works when the property name is a valid identifier.
- Bracket Notation: Useful when accessing properties dynamically or when the property name includes special characters or spaces.
Adding and Modifying Properties
You can add new properties or update existing ones.
JavaScript
let obj = {
name: 'Sourav',
age: 23,
isActive: true
};
obj.gender = 'male'; // Adding a new property
obj.age = 26; // Modifying an existing property
console.log(obj);
Output{ name: 'Sourav', age: 26, isActive: true, gender: 'male' }
Properties can be added or updated simply by assigning a value to a key.
Deleting Properties
Use the delete operator to remove a property from an object.
JavaScript
let obj = {
name: 'Sourav',
age: 23,
isActive: true
};
delete obj.isActive;
console.log(obj);
Output{ name: 'Sourav', age: 23 }
Checking Property Existence
You can check if a property exists using the in operator or hasOwnProperty() method.
JavaScript
let obj = {
name: 'Sourav',
age: 23,
gender: 'female'
};
console.log('age' in obj);
console.log(obj.hasOwnProperty('gender'));
- in Operator: Checks if the property exists in the object or its prototype chain.
- hasOwnProperty(): Only checks properties owned directly by the object.
Enumerable vs Non-Enumerable Properties
Properties can be marked as enumerable or non-enumerable using Object.defineProperty().
JavaScript
let obj = {
name: 'Sourav',
age: 23,
gender: 'male'
};
Object.defineProperty(obj, 'country', {
value: 'India',
enumerable: false
});
console.log(obj.country);
console.log(Object.keys(obj));
OutputIndia
[ 'name', 'age', 'gender' ]
Non-enumerable properties do not appear in for…in loops or Object.keys() results.
Property Attributes
Object properties have attributes that define their behavior, such as writable, configurable, and enumerable.
JavaScript
let obj = {
name: 'Sourav',
age: 23
};
Object.defineProperty(obj, 'status', {
value: 'active',
writable: false,
configurable: false,
enumerable: true
});
obj.status = 'inactive'; // Does not change due to `writable: false`
console.log(obj.status);
- writable: Determines if the value can be changed.
- configurable: Specifies if the property can be deleted or modified.
- enumerable: Indicates if the property shows up during enumeration.
Accessors (Getters and Setters)
Objects can have computed properties using getters and setters.
JavaScript
let obj = {
fName: 'Sourav',
lName: 'Sharma',
get fullName() {
return `${this.fName} ${this.lName}`;
},
set fullName(name) {
[this.fName, this.lName] = name.split(' ');
}
};
console.log(obj.fullName);
obj.fullName = 'Ravi Kumar';
console.log(obj.fName);
console.log(obj.lName);
OutputSourav Sharma
Ravi
Kumar
Getters allow reading, and setters enable modifying properties in a controlled manner.
Similar Reads
JavaScript Object Prototypes
JavaScript prototypes are used to access the properties and methods of objects. Inherited properties are originally defined in the prototype or parent object. The Date object is inherited from Date.prototype, Array object inherits from Array.prototype, etc. The prototypes may be used to add new prop
1 min read
JavaScript Object Reference
JavaScript Objects are the most important data type and form the building blocks for modern JavaScript. The "Object" class represents the JavaScript data types. Objects are quite different from JavaScriptâs primitive data types (Number, String, Boolean, null, undefined, and symbol). It is used to st
4 min read
JavaScript Objects
In our previous article on Introduction to Object Oriented Programming in JavaScript we have seen all the common OOP terminology and got to know how they do or don't exist in JavaScript. In this article, objects are discussed in detail. Creating Objects: In JavaScript, Objects can be created using t
6 min read
JavaScript Number Static Properties
The JavaScript Number is an object that can represent numeric values like integer, float, hexadecimal, decimal, etc. It acts as a wrapper object for primitive numeric values. It has various methods and properties that are used to perform different tasks on numbers. The following table shows some sta
2 min read
Properties in Objective-C
The object-oriented programming language Objective-C is largely used to create applications for Apple's macOS and iOS platforms. It incorporates all of the characteristics of C while also providing more features for object-oriented programming, making it a superset of the C programming language. The
5 min read
JavaScript Object propertyIsEnumerable() Method
The propertyIsEnumerable() method returns a Boolean indicating whether the specified property is enumerable and is the object's own property. The propertyIsEnumerable() method returns false if the object doesn't have the specified property. Syntax: obj.propertyIsEnumerable(prop) Parameters: This met
1 min read
Objects in Javascript
An object in JavaScript is a data structure used to store related data collections. It stores data as key-value pairs, where each key is a unique identifier for the associated value. Objects are dynamic, which means the properties can be added, modified, or deleted at runtime. There are two primary
4 min read
How to read properties of an Object in JavaScript ?
Objects in JavaScript, it is the most important data type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data-types(Number, String, Boolean, null, undefined, and symbol) in the sense that these primitive data-types all store a singl
2 min read
JSON vs JavaScript Object
JSON (JavaScript Object Notation) and JavaScript Objects are important for handling data in JavaScript, but they serve different purposes. JSON is a lightweight data format for transferring data, while JavaScript Objects are in-program data structures used for manipulation and logic. What is JSON?JS
3 min read
Rename Object Key in JavaScript
In JavaScript, objects are used to store the collection of various data. It is a collection of properties. A property is a "key: value" pair where Keys are known as 'property name' and are used to identify values. Since JavaScript doesn't provide an inbuilt function to rename an object key. So we wi
4 min read