The unary plus operator (+) precedes its operand and converts it into a number. If it fails to convert the operand into a number, then it returns NaN. The unary (-) operator converts the operand into a number and negates it.
Unary plus or minus operators require a single operand (hence the name unary) & precedes the operand.
In the following example, y is a string. We can place the unary plus (+) or minus (-) in front of it as shown below. It will convert it into a numbernumber and returns it.
let y = "1";
console.log(typeof(y)); //string
console.log(typeof(+y)); //number
console.log(+y); //1
console.log(-y); //-1
let y="-1"
console.log(+y); //-1
console.log(-y); //1
The unary plus or minus operators follows these rules
Unary plus converts the operand to a number. If it fails, then it returns NaN . The following shows how it works for different values.
console.log(+"100") //100
console.log(+"100.5175") //100.5175
//Empty string & null is zero
console.log(+"") //0
console.log(+" ") //0
console.log(+null) //0
//Undefined
console.log(+undefined) //Nan
//Infinity
console.log(+Infinity) //Infinity
//Boolean
console.log(+true) //1
console.log(+false) //0
//Hexadecimal
console.log(+"0x22") //34
console.log(+"0022") //22
console.log(+"0o51") //41
console.log(+"3.125e7") //31250000
//If fails to convert to number, then returns NaN
console.log(+"10AA0.5175") //NaN
console.log(+"35 35") //NaN
console.log(+"AB 35") //NaN
console.log(+'hello'); //NaN
The unary plus operator (+) precedes its operand and converts it into a number. If it fails to convert the operand into a number, then it returns NaN. The unary (-) operator converts the operand into a number and negates it.
Read More