Table of Contents
JavaScript objects are containers for named values, called properties, and methods. They are similar to real-life objects in that they have attributes (properties) and can perform actions (methods). An object is a collection of properties where each property is a key-value pair.
In JavaScript, an object is defined using curly braces {}. Objects can hold primitive data types, such as strings, numbers, and Booleans, as well as other objects and functions. Objects are one of the three primitive data types in JavaScript, along with strings and numbers.
Creating Objects in JavaScript
In JavaScript, objects can be created using the object literal notation or the constructor function.
The object literal notation is the simplest way to create an object in JavaScript. It is created by wrapping a comma-separated list of key-value pairs in curly braces {}.
let person = {
name: 'John',
age: 30,
address: {
street: '123 Main St',
city: 'Anytown',
state: 'CA',
zip: '12345'
}
};The constructor function creates an object using the new keyword and a function that defines the object’s properties and methods.
function Person(name, age) {
this.name = name;
this.age = age;
this.sayHello = function() {
console.log('Hello, my name is ' + this.name + ' and I am ' + this.age + ' years old.');
};
}
let person = new Person('John', 30);
person.sayHello(); // Output: Hello, my name is John and I am 30 years old.Object Properties
An object’s properties are the values associated with a given key. Properties can be added, updated, or deleted at any time.
Accessing Object Properties
Object properties can be accessed using dot notation or bracket notation.
let person = {
name: 'John',
age: 30
};
console.log(person.name); // Output: John
console.log(person['age']); // Output: 30
Bracket notation is useful when accessing properties with spaces in their name or when using a variable to access a property.
let person = {
'full name': 'John Smith',
age: 30
};
console.log(person['full name']); // Output: John Smith
let propertyName = 'age';
console.log(person[propertyName]); // Output: 30
Updating Object Properties
Bracket notation is useful whObject properties can be updated using dot notation or bracket notation.n accessing properties with spaces in their name or when using a variable to access a property.
let person = {
name: 'John',
age:
Adding Object Properties
Object properties can be added using dot notation or bracket notation.
let person = {
name: 'John',
age: 30
};
person.address = {
street: '123 Main St',
city: 'Anytown',
state: 'CA',
zip: '12345'
};
console.log(person); // Output: { name: 'John', age: 30, address: { street: '123 Main St', city: 'Anytown', state: 'CA', zip: '12345' } }
Deleting Object Properties
Object properties can be deleted using the delete keyword.
let person = {
name: 'John',
age: 30
};
delete person.age;
console.log(person); // Output: { name: 'John' }
Object Methods
An object’s methods are functions that are associated with the object. Methods are defined as object properties whose value is a function.
let person = {
name: 'John',
age: 30,
sayHello: function() {
console.log('Hello, my name is ' + this.name + ' and I am ' + this.age + ' years old.');
}
};
person.sayHello(); // Output: Hello, my name is John and I am 30 years old.
Best Practices for Accessing Properties in JavaScript
When accessing properties in JavaScript, it is important to follow some best practices to ensure that your code is readable and maintainable:
- Use dot notation whenever possible, as it is more concise and easier to read.
- Use bracket notation when the property name is stored in a variable or contains special characters.
- Always check if a property exists before accessing it, to avoid errors.
- Avoid accessing properties of nested objects in a single line, as it can make the code difficult to read and debug.

