Basic JavaScript Object

Posted by admin at April 2, 2020

Think about things people see every day, like cars, shops, and birds. These are all objects: tangible things people can observe and interact with.

What are some qualities of these objects? A car has wheels. Shops sell items. Birds have wings.

These qualities, or properties, define what makes up an object. Note that similar objects share the same properties, but may have different values for those properties. For example, all cars have wheels, but not all cars have the same number of wheels.

Objects in JavaScript are used to model real-world objects, giving them properties and behavior just like their real-world counterparts. Here’s an example using these concepts to create a duck object:

let duck = {
  name: "Aflac",
  numLegs: 2,
  color:"white"
};

This duck object has two property/value pairs: a name of “Aflac” and a numLegs of 2.

Dot Notation to Access the Properties of an Object

Dot notation is used on the object name, duck, followed by the name of the property, name, to access the value of “Aflac”.

let duck = {
  name: "Aflac",
  numLegs: 2
};
console.log(duck.name);

Comments

Write a Reply or Comment

Your email address will not be published. Required fields are marked *