# Unary Plus and Minus

+ is the unary plus. When the unary plus is applied to a nonnumeric value, it performs the same conversion as the Number() casting function.

``````
<!DOCTYPE html>
<html>
<title>Operator Example</title>
<script type="text/javascript">

var s1 = "2";
var s2 = "A";
var s3 = "2.1";
var s4 = "0xAA";
var b  =  true;
var b2 = false;
var f  = 1.1;
var o  = {
valueOf: function() {
return 10;
}
};

document.writeln(s1);
document.writeln(s2);
document.writeln(s3);
document.writeln(s4);
document.writeln(b);
document.writeln(b2);
document.writeln(f);
document.writeln(o);

s1 = +s1;
s2 = +s2;
s3 = +s3;
s4 = +s4;
b  = +b;
b2 = +b2;
f = +f;
o = +o;

document.writeln(s1);
document.writeln(s2);
document.writeln(s3);
document.writeln(s4);
document.writeln(b);
document.writeln(b2);
document.writeln(f);
document.writeln(o);

</script>

<body>

</body>
</html>

``````
Click to view the demo

The unary minus operator negates a numeric value.

``````
<!DOCTYPE html>
<html>
<title>Operator Example</title>
<script type="text/javascript">

var num = 2;
num = -num; //becomes -2
document.writeln(num);

</script>

<body>

</body>
</html>

``````
Click to view the demo

When used on nonnumeric values, unary minus applies the same rules as unary plus:

``````
<!DOCTYPE html>
<html>
<title>Operator Example</title>
<script type="text/javascript">

var s1 = "2";
var s2 = "A";
var s3 = "2.1";
var s4 = "0xAA";
var b  =  true;
var b2 = false;
var f  = 1.1;
var o  = {
valueOf: function() {
return 10;
}
};

document.writeln(s1);
document.writeln(s2);
document.writeln(s3);
document.writeln(s4);
document.writeln(b);
document.writeln(b2);
document.writeln(f);
document.writeln(o);

s1 = -s1;
s2 = -s2;
s3 = -s3;
s4 = -s4;
b  = -b;
b2 = -b2;
f = -f;
o = -o;

document.writeln(s1);
document.writeln(s2);
document.writeln(s3);
document.writeln(s4);
document.writeln(b);
document.writeln(b2);
document.writeln(f);
document.writeln(o);

</script>