Does anyone understand the meaning of adding + to variable names when multiplying by JavaScript?
In the following cases, calc1 and calc2 have the same result, but is it something magical?
class Data{
constructor(val1,val2){
This.val1 = val1
this.val2 = val2
}
calc1(){
return this.val1 *this.val2
}
calc2(){
return this.val1*+this.val2//< -- I don't understand this
}
}
const data = new data (100.12, 141.24)
data.calc1()
// =>14140.948800000002
data.calc2()
// =>14140.948800000002
// have the same result
Isn't it this?(Bold text quoted)
Single term addition (+) - JavaScript|MDN
The singular addition operator evaluates the operand before the operand, but tries to convert non-numeric values to numeric values. The singular negative (-) can also convert non-numeric values, but it is the fastest way to convert any value to numeric values.Supports integers in decimal and hexadecimal notation (prefix "0x").Negative numbers are also supported (except hexadecimal).If individual values cannot be interpreted, they are evaluated to NaN.
© 2024 OneMinuteCode. All rights reserved.