JavaScript Tutorial/Operators/Prefix operator

Материал из Web эксперт
Перейти к: навигация, поиск

Pre-Decrement

The pre-decrement operator (--) is placed to the left of a variable.

The pre-decrement operator decrements the value by 1.

If the variable is a string, it is converted to a number.

2. 3. Prefix operator 2. 3. 1. <A href="/Tutorial/JavaScript/0040__Operators/PreIncrement.htm">Pre-Increment</a> 2. 3. 2. Pre-Decrement 2. 3. 3. <A href="/Tutorial/JavaScript/0040__Operators/Prefixoperator.htm">Prefix operator: ++</a> 2. 3. 4. <A href="/Tutorial/JavaScript/0040__Operators/Prefixoperator2.htm">Prefix operator 2: --</a>

Prefix operator: ++

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
var a = 6, b = 2;
alert(++a + b);
//  -->
</script>
</head>
<body>
</body>
</html>


Prefix operator 2: --

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
var a = 6, b = 2;
alert(--a + b);
//  -->
</script>
</head>
<body>
</body>
</html>


Pre-Increment

The pre-increment operator is placed directly before the variable.

The operation begins by incrementing the variable by 1.

The new incremented value is returned by the operation to be used in another expression.

If the variable is a string, it is converted to a number.

For example, the following segment of code



var price = 5
var newPrice = (++price) + 3;