TS newbie ... common key value pairs
Hello again! This time it's codecademys TS course that is giving me headaches ...
How does
friendName
know to only take displayName
and log it? Or to ask it differently – how is newEvent
only picking the displayName
, if I understand this right ...9 Replies
Because in JS
||
and &&
are not boolean algebra.
In this example it takes a look at displayName, if it is a truthy value it gets returned, if it is falsy, it returns username.Hmm... not really sure if I understood this, but ...
friendName
is asking to return event.displayName
or event.username
, right?
And since event.displayName
is truthy (because it is a property in either Like
and Share
), it doesn't care about event.username
, and goes on with just event.displayName
. This then decided what property to use in newEvent
, and so on to the log ...
Right?To clarify,
||
and &&
ARE boolean operators. But ||
don't return true if any one of the value around it is true, it returns the first value if it's truthy or the second if not. So in your case, because you access displayName and then username it will return the former if it's not falsy (i.e. undefined or empty string mostly) and if it is it will return the value of username.
Almost but not quite. The properties of newEvent are set by the object litteral and you only pick from it the value that will be logged using the getFriendNameFromeEvent function
(and just so you know, this has nothing to do with typescript and everything to do with how JS works.)If you still have issues understanding this code, you can look at the MDN page for
||
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_ORLogical OR (||) - JavaScript | MDN
The logical OR () (logical disjunction) operator for a set of operands
is true if and only if one or more of its operands is true. It is typically used with
boolean (logical) values. When it is, it returns a Boolean value. However,
the operator actually returns the value of one of the specified
operands, so if this operator is used ...
Whoa ... 😮💨 Ok, let me try again to explain to myself ... I think I understand the
||
operator in this code, but not the newEvent
So this still holds:
friendName
is asking to return event.displayName
or event.username
, right?
And since event.displayName
is truthy (because it is a property in either Like and Share), it doesn't care about event.username
, and goes on with just event.displayName
.
But how is the poperty displayName: 'Veronica Krauss',
chosen if the consuequence of what is mentioned above is not in play, which is what I understand from your comment: "Almost but not quite. The properties of newEvent are set by the object litteral and you only pick from it the value that will be logged using the getFriendNameFromeEvent function"The username property would only be used if the displayName is falsy, and that can happen if the value for displayName is an empty string
""
(or undefined or null or 0...)
If you have a hard time figuring out how it all works try to remove all the typescript parts (so the type declaration and the : Like | Share
in the function signature)
Maybe it'll be clearerBut
displayName: 'Veronica Krauss'
is chosen because event.displayName
in the getFriendNameFromEvent
function is truthy, right?In this case yes
That was really the only thing left to understand.
Then it makes sense.
Thanks!
🙌