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.

JavaScript

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

Introduction to JavaScript JavaScript is

javaScript Statements In programming, a

Output in JavaScript JavaScript output

javaScript comments Comments in JavaScript

JavaScript Operators Operators in JavaScript

JavaScript Loops JavaScript loops are

JavaScript Performance and Debugging JavaScript

OOP in Javascript Object-Oriented Programming

Leave a Comment

Your email address will not be published. Required fields are marked *

// Sticky ads
Your Poster