How to group several variables together to create a basic data package in javascript

In JavaScript, you can create a new object using curly braces {}. Inside these braces, you list properties separated by commas. Each property has a name, a colon, and a value. You don't need to use the "new" keyword or specify the object type.

1. Students:

In this example, we have created a studentData object with properties such as name, age, major, GPA, and enrolledCourses. The properties store information about a student.

 
let studentData = {
    name: "John Doe",
    age: 20,
    major: "Computer Science",
    GPA: 3.8,
    enrolledCourses: ["Introduction to Programming", "Data Structures", "Algorithms"]
};
 
 

Product : 

An object literal that represents a product with some properties and a method to calculate the total price.

 
let product = {
  name: "Laptop",
  price: 1000,
  tax: 0.1,
  // A method to calculate the total price
  getTotalPrice: function() {
    // Add the tax to the price
    let total = this.price * (1 + this.tax);
    // Return the result
    return total;
  }
};
 
// Access the properties using dot notation or bracket notation
console.log(product.name); // Laptop
console.log(product["price"]); // 1000
 
// Call the method using dot notation
console.log(product.getTotalPrice()); // 1100
 

Companies:

 

const companies = {
   
  names: ["Google", "Microsoft", "Apple"],
 
  getCompanyNames: function() {
    return this.names.join(", ");
  },
 
  addCompany: function(name) {
    this.names.push(name);
  }
};
 console.log(companies.names);
 
 console.log(companies.getCompanyNames());
 
 companies.addCompany("Amazon");
 
console.log(companies.names);

 

Explanation:

  • The companies object has a property named names that contains an array of company names.
  • The getCompanyNames method returns a string representation of all company names.
  • The addCompany method adds a new company name to the list.
Products:

 
const product = {
  name: "Laptop",
  price: 999.99,
  available: true,
  applyDiscount: function(percentage) {
    this.price *= (1 - percentage / 100);
  },
  checkAvailability: function() {
    return this.available;
  }
};
 

Explanation:

  • Represents a product with properties for name, price, and available.
  • Has methods applyDiscount() to apply a discount to the price and checkAvailability() to check if the product is in stock.
Orders:

An object literal that represents an order with some properties and a method to display the summary.

 
let order = {
  id: 123,
  date: "2021-01-01",
  customer: "Bob",
  items: [
    {name: "Laptop", price: 1000, quantity: 1},
    {name: "Mouse", price: 20, quantity: 2},
    {name: "Keyboard", price: 50, quantity: 1}
  ],
shippingAddress: {
    street: "123 Main St",
    city: "Anytown",
    state: "CA",
    zip: "12345"
  },
  calculateShipping: function() {
    // Logic to calculate shipping costs based on shipping address
  },
  confirmOrder: function() {
    // Logic to process and confirm the order
  },
 
  // A method to display the summary
  showSummary: function() {
    console.log("The order id is " + this.id + ".");
    console.log("The order date is " + this.date + ".");
    console.log("The customer name is " + this.customer + ".");
    console.log("The order items are:");
    // Loop through the items array
    for (let item of this.items) {
      // Display the item details
      console.log(item.name + ": $" + item.price + " x " + item.quantity + " = $" + (item.price * item.quantity));
    }
  }
};
 
// Access the properties using dot notation or bracket notation
console.log(order.id); // 123
console.log(order["date"]); // 2021-01-01
 
// Call the method using dot notation
order.showSummary();