Javascript Objects and Arrays with examples

Objects:

- Objects are collections of key-value pairs.
- Objects are used to store and manipulate data.
- Objects can contain properties and methods.

Object Syntax:

- Object declaration:
    - const objectName = { key: value, key: value };
- Accessing properties:
    - objectName.key
- Adding properties:
    - objectName.newKey = value
- Deleting properties:
    - delete objectName.key

Examples:

- Objects:


const person = {
    name: "John",
    age: 30,
    occupation: "Developer"
};

console.log(person.name); // Output: John
person.country = "USA";
console.log(person); // Output: { name: "John", age: 30, occupation: "Developer", country: "USA" }
delete person.age;
console.log(person); // Output: { name: "John", occupation: "Developer", country: "USA" }


Arrays:

- Arrays are collections of values.
- Arrays are used to store and manipulate data.
- Arrays can contain values of any data type.

Array Syntax:

- Array declaration:
    - const arrayName = [value, value, value];
- Accessing elements:
    - arrayName[index]
- Adding elements:
    - arrayName.push(value)
- Deleting elements:
    - arrayName.splice(index, 1)

Examples:

- Arrays:


const colors = ["red", "green", "blue"];
console.log(colors[0]); // Output: red
colors.push("yellow");
console.log(colors); // Output: ["red", "green", "blue", "yellow"]
colors.splice(1, 1);
console.log(colors); // Output: ["red", "blue", "yellow"]


By mastering objects and arrays, you will be able to write more efficient and effective JavaScript code. Remember to practice and experiment with different examples to solidify your understanding.

Next Topic:

- Module 5: DOM Manipulation

In this module, we will explore the fundamentals of DOM manipulation in JavaScript. We will learn how to select and manipulate HTML elements, and how to use events to interact with the user.

Leave a Reply

Shopping cart0
There are no products in the cart!
Continue shopping
0