JavaScript Objects – How To Use Object And Their Properties?

In this guide, we will learn two sections of javascript one called javascript object and the second one is javascript properties. Both sections are top used part of js and to move forward on any other section you must know about it.

Let’s have a look on what is Javascript function object and how to call properties?

JavaScript Objects

In JavaScript the Objects are variables but in Objects can contain many values. Objects are container i.e. var car = {type: “Audi”, model: “A4”, color: “gray”};

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Objects</h2>
<p id="darkweb"></p>
<script>
// Create an object:
var car = {type:"Audii", model:"Ai4", color:"grayi"};
// Display some data from the object:
document.getElementById("darkweb").innerHTML = "The car is " + car.type;
</script>
</body>
</html>

JavaScript Object Properties

In JavaScript the pair of name: values is known as Object properties.

Propertyvalues
First nameRobert
Last nameAlbert
Age25
GenderMale

There are two ways to access the object properties:

  • objectName.propertyName

or

  • objectName[“propertyName”]

Example:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Objects</h2>
<p id="darkweb"></p>
<script>
// Create an object:
var person = {
  firstName: "Rob",
  lastName : "Alb",
  id     :  4555
};
// Display some data from the object:
document.getElementById("darkweb").innerHTML =
person.firstName + " " + person.lastName;
</script>
</body>
</html>

Hope above example will give you all idea about javascript object and properties. To get more javascript tutorials visit its page.

Stay in touch. Ask Questions.
Lean on us for help, strategies and expertise.

Leave a Reply

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