An object in JavaScript is a collection of key - value pairs, where the keys are the property names and the values can be of any data type. Property names are usually strings, but they can also be symbols. Here is a simple example of an object with properties:
const person = {
name: 'John Doe',
age: 30,
isStudent: false
};
In this example, name
, age
, and isStudent
are the property names, and 'John Doe'
, 30
, and false
are the corresponding property values.
There are two main ways to access object properties: dot notation and bracket notation.
Dot notation is the most common way to access properties when the property name is a valid identifier.
const person = {
name: 'John Doe',
age: 30
};
console.log(person.name); // Output: John Doe
Bracket notation is useful when the property name contains special characters, spaces, or when the property name is stored in a variable.
const person = {
'full name': 'John Doe',
age: 30
};
const propertyName = 'full name';
console.log(person[propertyName]); // Output: John Doe
You can add new properties to an existing object or modify the values of existing properties using both dot and bracket notations.
const car = {
make: 'Toyota',
model: 'Corolla'
};
// Adding a new property
car.year = 2020;
// Modifying an existing property
car.model = 'Camry';
console.log(car);
// Output: { make: 'Toyota', model: 'Camry', year: 2020 }
You can use the delete
operator to remove a property from an object.
const book = {
title: 'JavaScript: The Definitive Guide',
author: 'David Flanagan'
};
delete book.author;
console.log(book); // Output: { title: 'JavaScript: The Definitive Guide' }
You can use the in
operator or the hasOwnProperty
method to check if an object has a specific property.
const fruit = {
name: 'Apple',
color: 'Red'
};
console.log('name' in fruit); // Output: true
console.log(fruit.hasOwnProperty('color')); // Output: true
You can use a for...in
loop to iterate over all the enumerable properties of an object.
const student = {
name: 'Alice',
age: 22,
major: 'Computer Science'
};
for (let property in student) {
console.log(`${property}: ${student[property]}`);
}
Object destructuring allows you to extract properties from an object and assign them to variables in a more concise way.
const person = {
firstName: 'Bob',
lastName: 'Smith',
age: 25
};
const { firstName, age } = person;
console.log(firstName); // Output: Bob
console.log(age); // Output: 25
Choose property names that clearly describe the data they hold. This makes your code more readable and maintainable. For example, instead of using x
and y
in an object representing a point, use latitude
and longitude
.
// Bad practice
const point = {
x: 10,
y: 20
};
// Good practice
const location = {
latitude: 10,
longitude: 20
};
JavaScript has a set of reserved words, and using them as property names can lead to unexpected behavior. For example, avoid using class
, function
, or let
as property names.
If you want to prevent an object’s properties from being modified, added, or deleted, you can use methods like Object.freeze()
, Object.seal()
, or Object.preventExtensions()
.
const config = {
apiKey: 'abc123'
};
Object.freeze(config);
config.apiKey = 'def456'; // This assignment will be ignored
console.log(config.apiKey); // Output: abc123
JavaScript object properties are a fundamental part of the language, allowing you to organize and manipulate data in a flexible way. By understanding the basic concepts, usage methods, common practices, and best practices, you can write more efficient and maintainable JavaScript code. Whether you’re working on a small script or a large - scale application, proper use of object properties will help you build robust and reliable software.