I've come across an interesting Javascript phenomenon that I can't explain. Let's say I wanted to convert the current time into values ranging from 0.0 to 23.5, in 0.5 increments - ie. 0, 0.5, 1 1.5 ... 22, 22.5, 23, 23.5
This code does that:
const dateTimeNow = new Date();
let currentTime = dateTimeNow.getHours();
currentTime += (dateTimeNow.getMinutes() === 30) ? 0.5 : 0;
console.log(currentTime);
But this single line version fails to produce the correct value:
const dateTimeNow = new Date();
let currentTime = dateTimeNow.getHours() + (dateTimeNow.getMinutes() === 30) ? 0.5 : 0;
console.log(currentTime);
Hoping someone can explain why!