Dot vs Bracket Notation in Accessing JavaScript Object.
Learn when to use dot and bracket notation in JavaScript.
We can access the property of an object by
- Dot notation
- Bracket notation
Dot Notation
This is the most popular and most used way of accessing the property of an object. This makes code more readable. Dot notation is used most frequently.
Syntax: obj.property_name
var user = {name : "Mark"};user.name ; // "Mark"
When not to use dot
notation
Consider that we are having a property name as 123
var obj = {
'123' : 123
};
In the above case if we cannot access using
obj.123;
So, If the property name is not a valid identifier
, then we cannot access its value using .
notation.
What is an Identifier?
An identifier is a sequence of characters in the code that identifies a
variable
,function
, orproperty
. Generally name of function or variable or property name of Object
What is a valid Identifier?
- In JavaScript, identifiers are…