Javascript : Dot Notation

Hi all,

From here,
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-dot-notation
I don’t understand what does this sentence mean even though I know how to use dot notation.

Dot notation is what you use when you know the name of the property you’re trying to access ahead of time.

More on this,

…access ahead of time.

Thanks in advance!

PS: Thank you @DanCouper for correcting my ‘Javascript’ spelling.

(you don’t access ahead of time, you know ahead of time the name of the property you are trying to access)

if you know the name of the property and you use it directly, you can use dot notation, if instead the name of the property is in a variable you can’t use dot notation, but you need bracket notation

obj.nameProp will try to access the property with name of nameProp.
There are cases in which you can’t use dot notation even knowing exactly the name of the property
for example if the name of the property is "name Prop", then you absolutely need bracket notation: obj["name Prop"]

1 Like

Hi @ilenia,

Thanks for explanation.
One question, in what kind of condition(s) we need to assign property to a variable?

do you mean the property name?

there are ways to extract the property names from an object, for situations in which you nerd to iterate through the object properties, in that case you will have a variable holding the property names

1 Like

Hi,

Now I understand it.
Thank you. :smiley: