Programming with JavaScript

⌘K
  1. Home
  2. Docs
  3. Programming with JavaScri...
  4. Example Programs
  5. Object Properties

Object Properties

Example 9:

Create an object called store with the properties,

  • ‘name’ (string)
  • ‘location’ (String)
  • ‘isOpen’ (Boolean)
  • ‘departments’ ( array)

The values for the properties are: “MegaMart”, “123 Main Street, Chennai”, true and array of Groceries, Electronics, Clothing respectively.

Create and object method called getStoreInfo which returns “MegaMart is located at 123 Main Street, City. It is currently open.

  1. Create an object called ‘manager’ with the properties
    • ‘name’
    • ‘age’
    • ‘contact’
    • The values of the properties are: John, 35, and john.megamart.com
  2. Add this object as property to store
  3. Print the name of the strore
  4. Print whether the store is open or not
  5. Print the departments in the store
  6. Print the name of the manager

Code

const store = {
  name: "MegaMart",
  location: "123 Main Street, Chennai",
  isOpen: true,
  department: ["Groceries", "Electronics", "Clothing"],
  getStoreInfo: function () {
    return `${this.name} is located at ${this.location} and it is currently ${
      this.isOpen ? "open" : "closed"
    }`;
  },
};
/*
i)	Create an object called ‘manager’ with the properties
‘name’ 
‘age’
‘contact’
The values of the properties are: John, 35, and john.megamart.com
Add this object as property to store
*/
const manager = {
  name: "John",
  age: 24,
  contact: "john.megamart.com",
};

store.manager = manager;

// ii) Print the name of the store
console.log(`Name of the store is: ${store.name}`);

// iii) Print whether the store is open or not
console.log(store.getStoreInfo());

// iv)	Print the departments in the store
console.log(`Departments:${store.department}`);

// v)	Print the name of the manager
console.log(`Name of the manager: ${store.manager.name}`);

Output:

Loading

Views: 113

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments