How to read properties of an Object in JavaScript ?
Last Updated :
21 Feb, 2022
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 single value each (depending on their types).
In this article, we will see How can we read the properties of an object in JavaScript. There are different ways to call the properties of objects in Javascript.
Dot property accessor: In this, we use a dot property accessor to access the property of the object, the property must be a valid JavaScript identifier.Â
Syntax:
ObjectName.propertyName
Example:
JavaScript
<script>
var obj={
name : "Mohan Jain",
age : 21,
address : "Bhopal"
};
var name=obj.name;
console.log(name);
</script>
Output:
Mohan Jain
Using square brackets: In this, we use a square bracket to access the property of the object. It is the same as accessing the elements of an array using the square bracket.
Syntax:
ObjectName[propertyName]
Example:
JavaScript
<script>
var obj={
name : "Mohan Jain",
age : 21,
address : "Bhopal"
};
var name=obj[name];
console.log(name);
</script>
Output:
Mohan Jain
Object destructuring: In this, we read a property and assign its value to a variable without duplicating the property name. It is similar to array destructuring except that instead of values being pulled out of an array. The properties (or keys) and their corresponding values can be pulled out from an object.
Syntax:
var { propertyName } = ObjectName
Example:
JavaScript
<script>
var obj={
name : "Mohan Jain",
age : 21,
address : "Bhopal"
};
var { name } = obj;
console.log(name);
</script>
Output:
Mohan Jain
Accessing the property of Object using jQuery: Make sure you have jquery installed. We can use the jQuery library function to access the properties of Object. jquery.each() method is used to traverse and access the properties of the object.
Example:
JavaScript
<script>
const jsdom = require('jsdom');
const dom = new jsdom.JSDOM("");
const jquery = require('jquery')(dom.window);
var obj={
name : "Mohan Jain",
age : 21,
address : "Bhopal"
};
jquery.each(obj, function(key, element) {
console.log('key: ' + key + ' ' + 'value: ' + element);
});
</script>
Output:
Similar Reads
How to create object properties in JavaScript ? JavaScript is built on an object-oriented framework. An object is a collection of properties, where each property links a key to a value. These properties are not in any specific order. The value of a JavaScript property can be a method (function). Object properties can be updated, modified, added,
4 min read
How to add and remove properties from objects in JavaScript ? We will try to understand how to add properties to an object and how to add or remove properties from an object in JavaScript.Before we go and see the addition and removal of properties from an object let us first understand the basics of an object in JavaScript.Object:An object in JavaScript is a c
6 min read
How to get a subset of a javascript object's properties? To get the subset of properties of a JavaScript Object, we make use of destructuring and Property Shorthand. The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.Syntax: subset = (({a,
2 min read
How to Pass Object as Parameter in JavaScript ? We'll see how to Pass an object as a Parameter in JavaScript. We can pass objects as parameters to functions just like we do with any other data type. passing objects as parameters to functions allows us to manipulate their properties within the function. These are the following approaches: Table of
3 min read
How to print the content of an object in JavaScript ? To print the content of an object in JavaScript we will use JavaScript methods like stringify, object.values and loops to display the required data. Let's first create a JavaScript Object containing some key-values as given below: JavaScript // Given Object const obj = { name: 'John', age: 30, city:
3 min read