-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10. Objects .js
More file actions
38 lines (30 loc) · 1.33 KB
/
10. Objects .js
File metadata and controls
38 lines (30 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Objects in JS :
// Basically objects used for storing data in key-value pairs.
// 1. Objects are a collection of key-value pairs.
// 2. They can contain properties and methods.
// 3. Objects are mutable, meaning you can change their properties and methods after creation.
// 4. You can access object properties using dot notation or bracket notation.
// -------------------------------------------------------------------------------
// Example of an object in JavaScript:
let person = {
name: "Ayush",
age: 21,
isWorking: true,
greet: function() {
console.log("Hello, my name is " + this.name + " and I am " + this.age + " years old." + " Working as an SDE! ");
}
}
person.greet(); // Output: Hello, my name is Ayush and I am 21 years old. Working as an SDE
// -------------------------------------------------------------------------------
// Accessing properties of an object:
console.log(person.name); // Output: "Ayush"
// -------------------------------------------------------------------------------
let student = {
name: "Sanket",
age: 25,
subjects: ["Math", "Science", "History"],
getDetails: function() {
return `${this.name} is ${this.age} years old and studies ${this.subjects.join(", ")}.`;
}
};
console.log(student.getDetails()); // Output: John is 20 years old and studies Math,