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")
About: Been programming for over 20 years and loving it!
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")
He shoots, he scores! 👌
One must know operator precedence to avoid gotchas like this one.
Yes of course, but the point was to question people’s knowledge.
console.log(`Hello ${gender === 'M' ? "Sir" : “Ma'am"}`)
Haahah nice one! I remembered when I encountered this for the first time, took me a bit to wrap my head around it xD
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"