JSByte: What do you think will be the output of this JavaScript code?
Think about what will be the output when you log the following -
1. console.log( 2 * null) // 0 . null gets converted to 0.
2. console.log( "4" - 1) // 3. "4" gets converted to 4.
3. console.log( "4" + 1) // 41. + with a string, so concat
4. console.log( "5" * 2) // 10. "5" gets converted to 5.
5. console.log( "five" * 2) // NaN.
6. console.log( false == 0) // true
JS will perform type coercion - convert types to match each other, for these operations. It will try to make sense of the operands, try to convert them to string or number, and when something doesn't seem intuitive to convert to number, such as "five"
, it gives back NaN
.
On a personal note -
I was running low on motivation for side projects for quite a while, and I wanted to have a place where we could hang out and do our side projects together, while keeping each other motivated and accountable. So I created livestreaming sessions on Twitch where I stream my side project in pomodoro focussed sessions. It has been a lot of fun! You should come hang out! I livestream every Tuesday and Sunday.
Peace! ✌🏼