A JavaScript object is a data structure that stores data as key-value pairs. Some commonly used properties of a JavaScript object include constructor
, hasOwnProperty
, prototype
, toString
, valueOf
, isPrototypeOf
, propertyIsEnumerable
, and toLocaleString
.
These properties can be used to manipulate and interact with JavaScript objects in a variety of ways.
Property | Description |
---|---|
constructor | A reference to the constructor function that created the object. |
hasOwnProperty(property) | A method that returns a boolean indicating whether the object has a property with the specified name as a direct property of the object itself (as opposed to a property inherited from the object’s prototype chain). |
prototype | A reference to the prototype object that the object is linked to. This is used to inherit properties and methods from the prototype. |
toString() | A method that returns a string representation of the object. |
valueOf() | A method that returns the primitive value of the object. |
isPrototypeOf(object) | A method that returns a boolean indicating whether the object is a prototype of the specified object. |
propertyIsEnumerable(property) | A method that returns a boolean indicating whether the specified property can be enumerated (i.e., whether it will show up in a for…in loop). |
toLocaleString() | A method that returns a localized string representation of the object. |
toString() | A method that returns a string representation of the object. |
valueOf() | A method that returns the primitive value of the object. |
JavaScript object properties list example code
Simple example code of how you might use some of the properties of a JavaScript object:
// Create a simple object
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
getFullName() {
return `${this.firstName} ${this.lastName}`;
}
};
// Use some of the object's properties
console.log(person.firstName); //"John"
console.log(person.age); //30
console.log(person.getFullName()); //"John Doe"
console.log(person.hasOwnProperty("age")); //true
console.log(person.hasOwnProperty("toString")); //false
Output:
How to list the properties of a JavaScript object?
Answer: To list the properties of a JavaScript object, you can use the Object.keys()
method or the for...in
loop.
Using Object.keys()
The Object.keys()
method returns an array of a given object’s own property names in the same order as a for...in
loop.
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
email: "[email protected]"
};
const propertyNames = Object.keys(person);
console.log(propertyNames); // Output: ["firstName", "lastName", "age", "email"]
Using for...in
loop
The for...in
loop iterates over all the enumerable properties of an object, including its inherited properties from its prototype chain.
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
email: "[email protected]"
};
for (let propertyName in person) {
if (person.hasOwnProperty(propertyName)) {
console.log(propertyName); // Output: firstName, lastName, age, email
}
}
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