ECMAScript Unary Operators

A unary operator has only one parameter, which is the object or value to be operated on. They are the simplest operators in ECMAScript.

delete

The delete operator removes a reference to a previously defined object property or method. For example:

var o = new Object;
o.name = "David";
alert(o.name);	//Output "David"
delete o.name;
alert(o.name);	// Outputs "undefined"

In this example, deleting the name property means forcing the解除 of its reference, setting it to undefined (i.e., the value of an uninitialized variable).

The delete operator cannot delete properties and methods not defined by the developer. For example, the following code will cause an error:

delete o.toString;

Even if toString is a valid method name, this line of code will cause an error because toString() is an original ECMAScript method and not a method defined by the developer.

void

The void operator returns undefined for any value. This operator is usually used to avoid outputting values that should not be output, such as when calling a JavaScript function from an HTML <a> element. To do this correctly, the function cannot return a valid value, otherwise the browser will clear the page and only display the result of the function. For example:

<a href="javascript:window.open('about:blank')">Click me</a>

If you put this line of code into an HTML page and click the link within it, you can see "[object]" displayed on the screen.TIY

This is because the window.open() method returns a reference to the newly opened window. Then the object will be converted into the string to be displayed.

To avoid this effect, you can call the window.open() function using the void operator:

<a href="javascript:void(window.open('about:blank'))">Click me</a>

This makes the window.open() call return undefined, which is not a valid value and will not be displayed in the browser window.

Tip:Remember, functions that truly return nothing all return undefined.

Prefix increment/decrement operator

The two operators borrowed directly from C (and Java) are the prefix increment and prefix decrement operators.

The prefix increment operator is adding 1 to the value, with the form of placing two plus signs (+) before the variable:

var iNum = 10;
++iNum;

The second line of code increases iNum to 11, which is essentially equivalent to:

var iNum = 10;
iNum = iNum + 1;

Similarly, the prefix decrement operator subtracts 1 from the value, with the form of placing two minus signs (-) before the variable:

var iNum = 10;
--iNum;

In this example, the second line of code reduces the value of iNum to 9.

When using prefix operators, note that both increment and decrement operators occur before the calculation of the expression. Consider the following example:

var iNum = 10;
--iNum;
alert(iNum);	//outputs "9"
alert(--iNum);	//outputs "8"
alert(iNum);	//outputs "8"

The second line of code performs a decrement operation on iNum, and the third line of code displays the result ("9"). The fourth line of code performs another decrement operation on iNum, but this time the pre-decrement operation and the output operation appear in the same statement, displaying the result as "8". To prove that all decrement operations have been implemented, the fifth line of code outputs "8" again.

In arithmetic expressions, the precedence of the pre-increment and pre-decrement operators is the same, so they must be calculated in order from left to right. For example:

var iNum1 = 2;
var iNum2 = 20;
var iNum3 = --iNum1 + ++iNum2;	//equals "22"
var iNum4 = iNum1 + iNum2;		//equals "22"

In the previous code, iNum3 equals 22 because the expression to be calculated is 1 + 21. The variable iNum4 also equals 22, which is also 1 + 21.

Post-increment/Post-decrement operators

There are also two operators borrowed directly from C (and Java), namely the post-increment and post-decrement operators.

The post-increment operator also adds 1 to the value, indicated by placing two plus signs (++) after the variable:

var iNum = 10;
iNum++;

As expected, the post-decrement operator also subtracts 1 from the value, indicated by adding two minus signs (--) after the variable:

var iNum = 10;
iNum--;

The second line of code reduces the value of iNum to 9.

Unlike the prefix operators, the postfix operators perform increment or decrement operations after the expression containing them has been evaluated. Consider the following example:

var iNum = 10;
iNum--;
alert(iNum);	//outputs "9"
alert(iNum--);	//outputs "9"
alert(iNum);	//outputs "8"

Similar to the example with the prefix operators, the second line of code performs a decrement operation on iNum, and the third line of code displays the result ("9"). The fourth line of code continues to display the value of iNum, but this time the decrement operator is applied in the same statement. Since the decrement operation occurs after the expression has been evaluated, the number displayed by this statement is "9". After executing the fifth line of code, the alert function displays "8", because a post-decrement operation was performed between executing the fourth line of code and the fifth line of code.

In arithmetic expressions, the precedence of the post-increment and post-decrement operators is the same, so they must be calculated in order from left to right. For example:

var iNum1 = 2;
var iNum2 = 20;
var iNum3 = iNum1-- + iNum2++;	//equals "22"
var iNum4 = iNum1 + iNum2;		//equals "22"

In the preceding code, iNum3 is equal to 22 because the expression calculates 2 + 20. Variable iNum4 is also equal to 22, but it calculates 1 + 21 because both increment and decrement operations occur after iNum3 is assigned.

Unary Plus and Unary Minus

Most people are familiar with unary plus and unary minus, which have the same usage in ECMAScript as what you learned in high school mathematics.

Unary plus has no effect on numbers in essence:

var iNum = 20;
iNum = +iNum;
alert(iNum);	// Outputs "20"

This code applies unary plus to the number 20 and still returns 20.

Although unary plus has no effect on numbers, it has interesting effects on strings, converting them to numbers.

var sNum = "20";
alert(typeof sNum);	// Outputs "string"
var iNum = +sNum;
alert(typeof iNum);	// Outputs "number"

This code converts the string "20" to a real number. When the unary plus operator operates on a string, it calculates the string in a manner similar to parseInt(), the main difference being that only strings starting with "0x" (representing hexadecimal numbers) can be converted to decimal values by unary operators. Therefore, using unary plus to convert "010" always results in 10, while "0xB" will be converted to 11.

On the other hand, unary minus is simply negating a number (for example, converting 20 to -20):

var iNum = 20;
iNum = -iNum;
alert(iNum);	// Outputs "-20"

Similar to the unary plus operator, the unary minus operator will also convert a string to an approximate number and also negate the value. For example:

var sNum = "20";
alert(typeof sNum);	// Outputs "string"
var iNum = -sNum;
alert(iNum);		// Outputs "-20"
alert(typeof iNum);	// Outputs "number"

In the above code, the unary minus operator will convert the string "-20" to -20 (the unary minus operator handles hexadecimal and decimal values in a similar way to the unary plus operator, but it also negates the value).