简体   繁体   中英

+ unary operator in javascript

i have read this topic already : Explain +var and -var unary operator in javascript

but i still can't understand this simple code :

var a = 3;
console.log(-a);  // -3
console.log(+a);  //  3
a = -a;
console.log(a);  // -3
console.log(+a);  // -3

"The unary negation operator precedes its operand and negates it."

"The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already."

but i still can't figure why console.log(+a) return 3 the first time.

but i still can't figure why console.log(+a) return 3 the first time.

The value of a is 3 at that point.

The previous line, -a , takes the value of a , negates it and passes it to console.log . It doesn't assign the changed value back to a .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM