• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 6 pages
Other

Objects in JavaScript: A Complete Guide with Examples

Document preview thumbnail
Preview 2 out of 6 pages

This document covers objects in JavaScript, including how to create, access, and manipulate them. Learn about key-value pairs, methods, and advanced features like nested objects with clear examples.

Content preview

Objects in JavaScript
Objects in JavaScript are collections of key-value pairs. They are one of the most
important data structures and are used to represent and manipulate complex
data.

1. Creating Objects
Object Literal

The simplest way to create an object.

const person = {
name: "Alice",
age: 25,
greet: function() {
console.log(`Hello, my name is ${this.name}`);
}
};
person.greet(); // Output: Hello, my name is Alice

Object Constructor

Uses the Object class to create an object.

const person = new Object();
person.name = "Bob";
person.age = 30;
console.log(person); // Output: { name: 'Bob', age: 30 }

Using a Function Constructor

Defines a reusable blueprint for creating similar objects.

function Person(name, age) {
this.name = name;
this.age = age;
this.greet = function() {

, console.log(`Hello, my name is ${this.name}`);
};
}
const alice = new Person("Alice", 25);
alice.greet(); // Output: Hello, my name is Alice

Using Object.create

Creates an object with a specified prototype.

const prototype = { greet() { console.log("Hello!"); } };
const obj = Object.create(prototype);
obj.greet(); // Output: Hello!


2. Accessing Properties
Dot Notation

console.log(person.name); // Output: Alice

Bracket Notation

Used when property names are dynamic or contain special characters.

console.log(person["name"]); // Output: Alice

Dynamic Property Names

const prop = "age";
console.log(person[prop]); // Output: 25


3. Modifying Properties
Add, update, or delete properties dynamically.

person.location = "New York"; // Adding
person.age = 26; // Updating

Document information

Uploaded on
January 27, 2025
Number of pages
6
Written in
2024/2025
Type
Other
Person
Unknown
$5.69

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions