I've recently come across this intriguing line of code:
var date = +new Date;
console.log(date);
So I experimented with putting + before strings to better understand what was going on and this was the result:
var one = +"1"; // -> 1
var negativeTwo = -"2"; // -> -2
var notReallyANumber = +"number: 1"; // -> NaN
console.log(one, negativeTwo, notReallyANumber);
It seems that whenever a + sign or a - sign is placed before a string, it converts that string into a number, and if a negative sign is placed before a string, the resulting number will be negative.
Finally, if the string is not numeric, the result will be NaN.
Why is that?
How does putting + or - signs before a string convert it into a number and how does it also apply to new Date?
EDIT:
How does a
+sign affectnew Date? How does the valueWed Nov 07 2018 21:50:30 GMT-0500for example, convert into a numerical representation?