Categories
JavaScript Tutorials

Unary Operators

Operators that work on only one value are called unary operators. They are the simplest operators in ECMAScript.

Increment/Decrement

The increment and decrement operators are taken directly from C and come in two versions: prefix and postfix. The prefix versions of the operators are placed before the variable they work on; the postfix ones are placed after the variable. To use a prefix increment, which adds 1 to a numeric value, you place two plus signs (++) in front of a variable like this:

var age = 29;
++age;

In this example, the prefix increment changes the value of age to 30 (adding 1 to its previous value of 29). This is effectively equal to the following:

var age = 29;
age = age + 1;

The prefix decrement acts in a similar manner, subtracting 1 from a numeric value. To use a prefix decrement, place two minus signs (–) before a variable, as shown here:

var age = 29;
--age;

Here the age variable is decremented to 28 (subtracting 1 from 29). When using either a prefix increment or a prefix decrement, the variable’s value is changed before the statement is evaluated(In computer science, this is usually referred to as having a side effect.).

Consider the following:

var age = 29;
var anotherAge = --age + 2;
alert(age); //outputs 28
alert(anotherAge); //outputs 30

Unary Plus and Minus

The unary plus and minus operators are familiar symbols to most developers and operate the same way in ECMAScript as they do in high-school math. The unary plus is represented by a single plus sign (+) placed before a variable and does nothing to a numeric value, as shown in this example:

var num = 25;
num = +num; //still 25

When the unary plus is applied to a non numeric value, it performs the same conversion as the Number() casting function: the Boolean values of false and true are converted to 0 and 1, string values are parsed according to a set of specific rules, and objects have their valueOf() and/or toString() method called to get a value to convert.

Logical NOT

The logical NOT operator is represented by an exclamation point (!) and may be applied to any

value in ECMAScript. This operator always returns a Boolean value, regardless of the data type it’s used on. The logical NOT operator first converts the operand to a Boolean value and then negates it, meaning that the logical NOT behaves in the following ways:

  • If the operand is an object, false is returned.
  • If the operand is an empty string, true is returned.
  • If the operand is a nonempty string, false is returned.
  • If the operand is the number 0, true is returned.
  • If the operand is any number other than 0 (including Infinity), false is returned.
  • If the operand is null, true is returned.
  • If the operand is NaN, true is returned.
  • If the operand is undefined, true is returned.

The following example illustrates this behavior:

alert(!false); //true
alert(!"blue"); //false
alert(!0); //true
alert(!NaN); //true
alert(!""); //true
alert(!12345); //false