There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
It is not evaluated, The assignment is. When username is falsy (meaning empty string, None, or 0) OR skips over it to the next operand and returns that, regardless whether truthy or not.
Read up on short-circuiting and how it works in the case of AND (&&) and OR (||).
My train of thought (and it works exactly this way in some other languages):
username is false
‘Stranger’ is true
false || true equals true
so (username || ‘Strangeer’) should equal true
How does the JS interpreter work with this line of code?
Does it read it from left to right?
Logging and assigning are evaluated the same way. When it comes to a conditional expression such as if or while or for then the action branch depends on the truthiness of the condition, not the literalness.
Not sure I see where there is any confusion with this exercise. The lesson narrative describes how OR is short-circuited by the first truthy value, reading from left to right, and that value is what is yielded in an assignment, an if conditional, or a while conditional, although the latter are not stated, only inferred. If none of the values are truthy, then the final operand is yielded.
This line: let defaultName = username || ‘Stranger’;
Can I say that it checks if var ‘username’ is truthy, if it is, it assigns its value to var ‘defaultName’, if var ‘username’ is falsy it assigns the value ‘Stranger’ to ‘defaultName’ ?
Can some please clarify the use of quotations. When to use/not use. This particular exercise threw me only because I put quotes around both “tool” and “pen”. When only “pen” should have been encased. Thank you!!!