0

Can someone explain this code?

if(+'15' === 15) { document.write('true'); } else if('15' === 15) { document.write('false'); } else { document.write('soloLearn'); } Why it evaluates to true instead of sololearn? What does that + sign stands for in the first condition block. I tried to google it but can't find the answer. Does it converts the string 15 into a number?

3rd Jan 2021, 12:03 PM
Pawan
Pawan - avatar
4 Answers
+ 1
"Does it convert string 15 into a number?" Yes 👍
3rd Jan 2021, 12:30 PM
Ipang
+ 1
Ipang Thanks for the confirmation.
3rd Jan 2021, 12:55 PM
Pawan
Pawan - avatar
+ 1
+ right before a numerical string just converts it to a number (or tries to). So you're essentially asking if '15', converted to a number, has the same value and type as 15 (a number)
3rd Jan 2021, 7:32 PM
HealyUnit
HealyUnit - avatar
0
HealyUnit Exactly
4th Jan 2021, 2:40 AM
Pawan
Pawan - avatar