Objects
Introduction
JavaScript object is a non-primitive data-type that means it allows you to store multiple collections of data.
When to Use Objects
Objects are used to represent a “thing” in your code. That could be a name, a person, a car, a book, a table — basically anything that is made up or can be defined by a set of characteristics. In objects, these characteristics are called properties
Properties consists of a Key and a Value .
Syntax
Example
Here,
name and age are keys and, ram and 19 are Values. Combinely Termed as Properties.
Parameter Representation
Accessing Object Values
For This, You Have two ways to access the Values
- Dot (.) Representations
Bracket( [ ] ) Representation
Example:
Note : Both will Result Same.
Assigning Value to Properties
You can add new properties to an existing object by simply giving it a value.
Deleting Properties
The delete keyword deletes a property from an object:
Syntax
Example :
JavaScript for...in Loop
The JavaScript for...in statement loops through the properties of an object to Print and Perform Action on Each Object Property.
Syntax :
The block of code inside of the for...in loop will be executed once for each property.
Example 2:
Here,
First, We have declared the Object monthlySalary and then assigned salaries of the person.
for...in loop will Iterate through all the Elements of the Object. i.e. it will print all the salaries of Persons.