Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

JavaScript Objects

JavaScript objects are collections of key-value pairs where keys are strings or Symbols and values can be any data type. Objects can be created using object literals, the Object constructor, or Object.create() for prototype-based inheritance. Properties can be accessed and modified using dot or bracket notation, and methods can be defined within objects to perform actions related to the object.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

JavaScript Objects

JavaScript objects are collections of key-value pairs where keys are strings or Symbols and values can be any data type. Objects can be created using object literals, the Object constructor, or Object.create() for prototype-based inheritance. Properties can be accessed and modified using dot or bracket notation, and methods can be defined within objects to perform actions related to the object.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

JavaScript Objects

1. What is an Object?
An object in JavaScript is a collection of key-value pairs where the keys
(properties) are strings (or Symbols), and the values can be any data type.

let person = {
name: "John",
age: 30,
isEmployed: true
};
2. Creating Objects
Using Object Literals (Most Common)

let car = {
brand: "Toyota",
model: "Camry",
year: 2022
};
Using the new Object() Constructor

let user = new Object();


user.name = "Alice";
user.age = 25;
Using Object.create() (Prototype-Based)

let parent = {
greet: function() {
console.log("Hello!");
}
};
let child = Object.create(parent);
child.name = "Kid";
console.log(child.name); // Kid
child.greet(); // Hello!
3. Accessing and Modifying Properties
Dot Notation (Recommended)

console.log(person.name); // John
person.age = 31;
Bracket Notation (Useful for dynamic keys)

console.log(person["name"]); // John
let key = "age";
console.log(person[key]); // 31
Adding and Deleting Properties

person.city = "New York"; // Add new property


delete person.isEmployed; // Remove a property
4. Object Methods (Functions inside Objects)

let student = {
name: "Emma",
greet: function() {
console.log(`Hello, my name is ${this.name}`);
}
};
student.greet(); // Hello, my name is Emma
📝 this refers to the current object.

You might also like