In JavaScript, objects are collections of key-value pairs. The keys are also called properties, and the values can be any data type (e.g., string, number, array, function).
1. Creating an Object with Properties
You can create an object and define properties in two ways:
Using Object Literal Syntax
const person = {
name: 'Alice',
age: 30,
job: 'Engineer'
};
console.log(person.name); // Output: Alice
console.log(person.age); // Output: 30
console.log(person.job); // Output: Engineer
Explanation:
- The object
personhas three properties:name,age, andjob, each assigned a value.
Using the new Object() Syntax
const person = new Object(); person.name = 'Bob'; person.age = 25; person.job = 'Designer'; console.log(person.name); // Output: Bob
Explanation:
- This approach creates an empty object and then adds properties (
name,age,job) to it.
2. Accessing Object Properties
There are two main ways to access the properties of an object:
Dot Notation
const person = {
name: 'Charlie',
age: 35
};
console.log(person.name); // Output: Charlie
console.log(person.age); // Output: 35
Explanation:
- With dot notation, you use the object’s property name directly.
Bracket Notation
console.log(person['name']); // Output: Charlie console.log(person['age']); // Output: 35
Explanation:
- Bracket notation is useful if the property name is stored in a variable or contains special characters (like spaces or dashes).
3. Modifying Object Properties
You can modify the properties of an object by directly assigning a new value to them.
const person = {
name: 'David',
age: 40
};
// Modifying properties
person.name = 'Daniel';
person.age = 42;
console.log(person.name); // Output: Daniel
console.log(person.age); // Output: 42
Explanation:
- By assigning a new value to a property, you update that property.
4. Adding New Properties to an Object
You can add new properties to an object after it has been created.
const person = {
name: 'Eva',
age: 28
};
// Adding new properties
person.job = 'Teacher';
person.city = 'London';
console.log(person.job); // Output: Teacher
console.log(person.city); // Output: London
Explanation:
- Adding a new property is as simple as assigning a value to a non-existent property.
5. Deleting Object Properties
You can remove a property from an object using the delete operator.
const person = {
name: 'Grace',
age: 22,
job: 'Artist'
};
// Deleting a property
delete person.job;
console.log(person.job); // Output: undefined
Explanation:
- The
deleteoperator removes thejobproperty from thepersonobject.
6. Checking if a Property Exists
To check if a property exists in an object, you can use the in operator or hasOwnProperty() method.
Using the in Operator
const person = {
name: 'Helen',
age: 26
};
console.log('age' in person); // Output: true
console.log('job' in person); // Output: false
UsinghasOwnProperty() Method
console.log(person.hasOwnProperty('name')); // Output: true
console.log(person.hasOwnProperty('job')); // Output: false
Explanation:
- The
inoperator checks if the property exists in the object (including in the prototype chain). hasOwnProperty()only checks for properties that belong directly to the object.
7. Iterating Over Object Properties
You can loop through an object’s properties using the for...in loop.
const person = {
name: 'Ivy',
age: 31,
job: 'Scientist'
};
for (let key in person) {
console.log(key + ': ' + person[key]);
}
Output:
name: Ivy age: 31 job: Scientist
Explanation:
- The
for...inloop iterates over all the keys in the object. You can access each key’s value by usingperson[key].
8. Object Property Shorthand
If the property name is the same as the variable name, you can use property shorthand.
const name = 'James';
const age = 27;
const person = { name, age };
console.log(person.name); // Output: James
console.log(person.age); // Output: 27
Explanation:
- Instead of writing
name: nameandage: age, JavaScript automatically assumes that the property name is the same as the variable name.
9. Nested Objects
Objects can have other objects as their properties.
const person = {
name: 'Kara',
address: {
street: '123 Main St',
city: 'New York',
zip: '10001'
}
};
console.log(person.address.city); // Output: New York
Explanation:
- The
addressproperty is itself an object, and you can access its properties using dot notation.
Summary of JavaScript Object Properties
- Objects store data in key-value pairs, and you can define, access, modify, and delete properties.
- Dot notation and bracket notation are used to access and modify object properties.
- You can add new properties to objects, delete existing ones, and check if a property exists using the
inoperator orhasOwnProperty(). - Object methods can be defined inside objects to perform operations on the object’s properties.
- Nested objects allow for more complex structures, and you can access inner properties with dot notation.