"return arg1*+arg2" What does the plus sign mean in this expression?

Asked 3 months ago, Updated 3 months ago, 6 views

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

javascript

2022-09-30 11:16

1 Answers

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.


2022-09-30 11:16

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.