Lesson 11: Syntax Explanation for src={pics.doggy} versus src=pics'doggy'?


#1

Hi, quick syntax (or maybe background operation) question!

Not stuck in an exercise, just looking for an explanation.

Valid Code:  <img src={pics[coinToss()==='heads' ? 'kitty' : 'doggy']} />;

Valid Code:  <img src={pics.doggy} />;

Not Valid Code:  <img src={pics'doggy'} />;

(So, to me, it seems obvious that whatever happens in the square brackets transforms the ‘var’ to .var
This is not explained, and so was looking for an explanation. Thanks.)


#2

Are you familiar with JS? I might hope so if you do reactJS course

we can access object by doing .propertyName or ['propertyName'], ['propertyName'] is known associative array notation, we always use square brackets when accessing array

explained here:

https://www.codecademy.com/en/courses/spencer-sandbox/1/4?curriculum_id=506324b3a7dffd00020bf661

the confusing thing is that objects are the same as associative array:

https://www.quirksmode.org/js/associative.html


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.