logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
Action Movies
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel
Southwest USA


dailyclick
All times in EST

Full Schedule
g
g JavaScript / Java Site

BellaOnline's JavaScript / Java Editor

g

JavaScript Increment and Decrement

Guest Author - Julie L Baumler

JavaScript has a number of arithmetic operators that are similar to what you know from elementary math, but it also has some that are specific only to programming languages. The most common and useful of these are probably the increment and decrement operators. If you've looked at any JavaScript code, you have very likely seen these operators used on counters (often called i or cnt by convention), such as i++.

The Increment and Decrement Operators
OperatorSymbolPurposeExampleResult
Increment
++
increment by 1
4++
5
Decrement
--
decrement by 1
4--
3


The interesting and sometimes confusing thing about the increment and decrement operators is that they have two forms prefix and postfix notation which act slightly differently. Prefix notation is when the operator ("++" or "--") is in front of the number or variable for instance, --i or ++cnt. Postfix notation is when the operator ("++" or "--") is placed after the number or variable for instance, i++ or cnt--.

When used in an expression (equation), prefix notation means the increment or decrement takes place before the number or variable is used. In postfix notation, the original value of the number or variable is used and then the increment or decrement takes place. This is easiest to understand by looking at how it works.

Prefix NotationPostfix Notation
Codea=4
b=++a + 2
a=4
b=a++ + 2
ResultThe increment takes place first resulting in the following values:
a=5
b=7
The addition take place first resulting in the following values:
a=5
b=6


Prefix and postfix notation can be confusing, however, a few things make it easier. In the vast majority of cases, the increment and decrement operators are used on their own or in parenthesis. When the increment and decrement operators are used on their own (as they often are when used as loop counters), the prefix and postfix versions work identically. The same is true when they are used in parenthesis. To avoid confusing yourself or other people reading your code, it's a good idea to use increment and decrement operators in expressions sparingly and with parenthesis whenever possible.
This site needs an editor - click to learn more!

Add JavaScript+Increment+and+Decrement to Twitter Add JavaScript+Increment+and+Decrement to Facebook Add JavaScript+Increment+and+Decrement to MySpace Add JavaScript+Increment+and+Decrement to Del.icio.us Digg JavaScript+Increment+and+Decrement Add JavaScript+Increment+and+Decrement to Yahoo My Web Add JavaScript+Increment+and+Decrement to Google Bookmarks Add JavaScript+Increment+and+Decrement to Stumbleupon Add JavaScript+Increment+and+Decrement to Reddit




JavaScript articles
JavaScript Pop-Ups
Comments in JavaScript
RSS
Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map


For FREE email updates, subscribe to the JavaScript / Java Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2014 by Julie L Baumler. All rights reserved.
This content was written by Julie L Baumler. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.

g


g features
Hearing Dog Tool - Adding Randomness

Hearing Dog Training Tool setTimeout()

GCJ - an Independant Java Implementation

Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2014 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor