Dot and Square Bracket Notation

前端 未结 2 2025
后悔当初
后悔当初 2020-12-04 03:55

I\'m trying to understand the difference between the Dot and the Square Bracket Notation. While going through various examples here on SO and on some other sites, I came acr

2条回答
  •  天命终不由人
    2020-12-04 04:34

    Accessing/Creation of properties from/in a JavaScript object can be done in two ways

    1. Using Dot notation
    2. Using Square Bracket notation

    Whenever some property is not defined i.e. not present in the object and you try to access it, you will get undefined (obviously, because it's not there).

    So, in the first example you are accessing a property and in the second example you are creating a property. Therefore, replacing the notation did not affect the code in the second example.

提交回复
热议问题