21

I'm trying to understand unary operators in javascript, I found this guide here http://wiki.answers.com/Q/What_are_unary_operators_in_javascript most of it makes sense but what I don't understand is how the following examples would be used in an actual code example:

+a;
-a;

To my understanding the +a; is meant to make the variable the positive value of a and the -a; is meant to make the variable the negative value of a. I've tried a number of examples like:

a = -10;
a = +a;
document.writeln(a);

And the output is still -10;

I've also tried:

a = false;
a = +a;
document.writeln(a);

And the output is 0;

What is a practical code example of these unary operators?

fakeguybrushthreepwood
  • 2,983
  • 7
  • 37
  • 53
  • 1
    You may find the description in the [MDN documentation](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Arithmetic_Operators#-_%28Unary_Negation%29) helpful. There it says *"The unary negation operator precedes its operand and negates it."* and *"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."* – Felix Kling Aug 25 '12 at 10:20

7 Answers7

28

The + operator doesn't change the sign of the value, and the - operator does change the sign. The outcome of both operators depend on the sign of the original value, neither operator makes the value positive or negative regardless of the original sign.

var a = 4;
a = -a; // -4
a = +a; // -4

The abs function does what you think that the + opreator does; it makes the value positive regardless of the original sign.

var a =-4;
a = Math.abs(a); // 4

Doing +a is practically the same as doing a * 1; it converts the value in a to a number if needed, but after that it doesn't change the value.

var a = "5";
a = +a; // 5

The + operator is used sometimes to convert string to numbers, but you have the parseInt and parseFloat functions for doing the conversion in a more specific way.

var a = "5";
a = parseInt(a, 10); //5
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Thanks for the feedback. I know can see how -a would be used, but sorry to sound daft @Guffa, but can you provide a super basic example of +a used in code? I'm trying to visualise how it would be used in a real world example. – fakeguybrushthreepwood Aug 25 '12 at 10:40
  • Hmm @guffa, sorry but I still can not see what a really practical example for +a would be - especially since you are saying that `parseInt` and `parseFloat` are superior ways to convert a string to a number. – fakeguybrushthreepwood Aug 25 '12 at 11:02
  • @u1sonderzug: Yes, there isn't really much practical use for the `+` unary operator, but other C-style languages has it, so I guess they wouldn't want to exclude it from Javscript. I have used Javscript for about 20 years, and I can't recall that I ever used the `+` unary operator. – Guffa Aug 25 '12 at 18:00
4

One example is that they can be used to convert a string to a number,

var threeStr = '3.0'
var three = +threeStr
console.log(threeStr + 3) // '3.03'
console.log(three + 3) //6
phenomnomnominal
  • 5,427
  • 1
  • 30
  • 48
2

I would like to explain this from basic mathematical point:
The multiplying rules:

Positive x Positive = Positive: 3 x 2 = 6  
Negative x Negative = Positive: (-2) x (-8) = 16   
Negative x Positive = Negative: (-3) x 4 = -12  
Positive x Negative = Negative: 3 x (-4) = -12  

Considering you example:

a = -10;
a = +a
document.writeln(a);

+a = +(-10) = Positive x Negative = Negative = -10

a = false;
a = +a;
document.writeln(a);

false == 0, +a = +(+0) = Positive * Positive = Positive = 0 (maybe use true is a better example)

Baiyan Huang
  • 6,463
  • 8
  • 45
  • 72
0
a = 1
b = -a
console.log(b)

output
-1
Drazen
  • 2,776
  • 1
  • 25
  • 39
0

'+' operator in a variable 'a' simply means : a

'-' operator in a variable 'a' simply means : -a

Since, in above example 
a=-10;
a= +a; // means a, ie, +(-10) which is -10

but,
a= -a;  // means -a, ie, -(-10) which is +10
Mohit Pandey
  • 3,679
  • 7
  • 26
  • 38
0

+a means a*1 and -a means a*(-1)

Thats it!!!!!!

Rupesh Patel
  • 3,015
  • 5
  • 28
  • 49
-1

Try this

false == 0 // returns true

So,

a = false

a = +a //a * 1

console.log(a) // prints 0 as expected
Robin Maben
  • 22,194
  • 16
  • 64
  • 99