A JavaScript object is a non-primitive data type that stores multiple values in the form of properties and methods.
Objects are collections of key-value pairs.
JavaScript provides two common ways to create objects.
Both methods create similar objects.
Object literals are the easiest and most commonly used way to create objects.
Objects can also contain functions called methods.
Methods perform actions using object properties.
Every property must have a value.
Always use key:value pairs.
Objects can also be created using the Object() constructor and the new keyword.
Properties can be added using dot notation or square brackets.
Variables can also be used as object properties.
JavaScript automatically creates properties with the same variable names.
Object properties can be accessed in two ways.
Both methods return the property value.
Methods are functions stored inside objects.
Methods are called using parentheses ().
Use hasOwnProperty() to check whether a property exists.
If the property does not exist, JavaScript returns undefined.
Objects are assigned by reference.
Changing p2 also changes p1 because both refer to the same object.
Use the for...in loop to access all properties of an object.
The loop accesses both property names and their values.
Objects are passed by reference in JavaScript.
The original object changes because the function receives its reference.
An object can contain another object.
Nested objects help organize complex data.
| Concept | Example |
|---|---|
| Create Object | { } |
| Constructor | new Object() |
| Access Property | obj.name |
| Access Method | obj.method() |
| Check Property | hasOwnProperty() |
| Loop | for...in |
Which syntax is used to create an object literal in JavaScript?