JavaScript Object
An object in JavaScript is a complex data structure that groups related data together using the key-value pairs.
Syntax :
let object_name = {
key1 : value1,
key1 : value1,
...
};
Example: In this example, The person is an object with the properties like name, age, hobbies and address. It includes both the primitive data types and other objects.
Javascript
let person = { name: 'Kumar' , age: 25, hobbies: [ 'reading' , 'painting' ], address: { street: '127 Main St' , city: 'Bangalore City' } }; console.log( "Name: " + person.name); console.log( "Age: " + person.age); console.log( "Hobbies: " + person.hobbies.join( ', ' )); console.log( "Street: " + person.address.street); console.log( "City: " + person.address.city); |
Output:
Name: Kumar
Age: 25
Hobbies: reading, painting
Street: 127 Main St
City: Bangalore City
Difference Between Variables and Objects in JavaScript
The variables and objects are fundamental concepts but they serve different purposes. The Variables are used to store data values while objects are used to group related data and functions into a single entity.
Contact Us