JavaScript Lesson 4: Objects, Arrays, and Methods
Page 1: Introduction to Objects in JavaScript
What is an Object?
o In JavaScript, an object is a collection of properties and values.
o Objects allow you to group related data and functionality in a single structure.
Example:
Accessing Object Properties:
You can access object properties using dot notation or bracket notation.
Example:
console.log(person.name); // Outputs: Alice
console.log(person['age']); // Outputs: 25
Modifying Object Properties:
You can add or change properties in an object dynamically.
Example:
Page 2: Methods in Objects
What is a Method?
o A method is a function that is a property of an object.
Example:
The this Keyword in Methods:
In a method, this refers to the object calling the method.
Example:
Page 3: Introduction to Arrays
What is an Array?
o An array is a collection of ordered values. Arrays are used to store multiple values in a
single variable.
Example:
Accessing Array Elements:
You can access array elements using their index (starting from 0).
Example:
Modifying Arrays:
You can modify arrays by adding, removing, or changing elements.
Example:
Page 4: Array Methods
Common Array Methods:
o push(): Adds a new element to the end of the array.
o pop(): Removes the last element from the array.
o shift(): Removes the first element from the array.
o unshift(): Adds a new element to the beginning of the array.
Example: