Hello, coders! π»
Quick quiz for you: what is the output of the code below and why?
const gender = 'M';
console.log("Hello " + gender === 'M' ? "Sir" : "Ma'am")
Hello, coders! π»
Quick quiz for you: what is the output of the code below and why?
const gender = 'M';
console.log("Hello " + gender === 'M' ? "Sir" : "Ma'am")
For further actions, you may consider blocking this person and/or reporting abuse
Jaime -
Probir Sarkar -
Agbo, Daniel Onuoha -
keploy -
Top comments (7)
Hey Luke. Thanks for your comment. As you said there was no bad intention, it was just a code example. I'll be more careful next time.
Above code would be better with template literals indeed but the point of the quiz was to show that codes doesn't always work like you think it will. I think I saw it when doing code review and thought it was worth sharing. :)
"Hello " + gender != 'M'
The output is "Ma' am"
He shoots, he scores! π
One must know operator precedence to avoid gotchas like this one.
Tip:
Use Template strings
Yes of course, but the point was to question peopleβs knowledge.
Haahah nice one! I remembered when I encountered this for the first time, took me a bit to wrap my head around it xD
Ha ha I can relate :)