Simple JavaScript : Accessing properties of objects.

There are mainly two ways to access a property in JavaScript.
  • Using dot notation (.)
  • Using square bracket notation ([])
Consider an object like this.

var employee={
    name:"Antony",
    age:26
}

We can access properties of employee object in both ways.
  1. employee.name
  2. employee["name"]
But Consider a object that has non-string property names. Some times we have to use object property names with non-string types.
Ex:
var marks={
    30:"bad",
    70:"good"
}

In here we won't be able to properties with both notation. Only marks["30"] will work.

No comments:

Post a Comment