Javascript

JavaScript Objects

JavaScript Objects

In JavaScript, objects are complex data types that allow you to store and organize data in key-value pairs. Objects are used to represent real-world entities, and they are a fundamental part of the language.

In the below PDF we discuss about JavaScript  Objects in detail in simple language, Hope this will help in better understanding.

Creating Objects:

There are two main ways to create objects in JavaScript:

1. Object Literal Syntax:


// Object literal syntax
let person = {
name: "John",
age: 30,
city: "New York"
};

2. Object Constructor Syntax:


// Object constructor syntax
let person = new Object();
person.name = "John";
person.age = 30;
person.city = "New York";

Object Examples:

let person = {
firstName: "John",
lastName: "Doe",
age: 30,
isStudent: false,
address: {
street: "123 Main St",
city: "Anytown",
zipCode: "12345"
},
sayHello: function() {
console.log("Hello, " + this.firstName + "!");
}
};

In this example:

firstName, lastName, age, and isStudent are properties of the person object.
address is an object property containing nested key-value pairs.
sayHello is a method (a function property) associated with the person object.

Accessing Object Properties:

You can access the properties of an object using dot notation or square bracket notation:

console.log(person.name); // Dot notation
console.log(person[“age”]); // Square bracket notation
Object Methods:
Objects can contain functions as values, and these are called methods. Methods are functions associated with an object:

let car = {
brand: "Toyota",
model: "Camry",
start: function() {
console.log("Engine started.");
},
stop: function() {
console.log("Engine stopped.");
}
};

car.start();
car.stop();

Nested Objects:

Objects can contain other objects as properties, creating nested structures:

let person = {
name: "John",
address: {
street: "123 Main St",
city: "Anytown",
zip: "12345"
}
};

console.log(person.address.city); // Accessing nested property

Related Question

A JavaScript Object is a complex data type that allows you to store and organize data using key-value pairs. It is a collection of properties, where each property has a name (key) and a value.

You can create a JavaScript object using curly braces {}.

 

Properties are the key-value pairs in an object, representing the characteristics or attributes of the object. Methods are functions associated with an object, allowing it to perform actions.

You can access the values of an object’s properties using dot notation or square bracket notation.

Relevant

The current query has no posts. Please make sure you have published items matching your query.

Premium E-Books & Study Materials.

Access university-specific notes and programming guides. Download instantly to start studying.

Share Article :
Python DSA Ebooks cover

TopperWorld
Prime E-Books

Master programming and core CS concepts with expert-curated study guides.

Recent Tutorials :

Best Hostinger Discount Code 2025 | Hostinger Referral Code For Extra Discount
Java Most Asked Interview Question and Answer
Top 10 AI companies in India
How Generative AI is Transforming Programming
Top GITHUB Interview Questions and Answers