
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?
The increment and decrement operators should be avoided since it can lead to unexpected results. Here are some of the conditions:
In an assignment statement, it can lead to unfavorable results:
Example
<html> <body> <script> var a = 5; var b = ++a; var c = a++; var d = ++c; document.write(a); document.write("\r"+b); document.write("\r"+c); document.write("\r"+d); </script> </body> </html>
Output
Whitespace between the operator and variable can also lead to unexpected results:
a = b = c = 1; ++a ; b -- ; c;
- Related Articles
- Python Increment and Decrement Operators
- Increment and decrement operators in Java
- Increment and Decrement Operators in Python?
- Increment ++ and decrement -- Operators in C++
- Increment and Decrement Operators in C#
- PHP Increment/Decrement Operators
- What are increment (++) and decrement (--) operators in C#?
- Interesting facts about Increment and Decrement operators in Java
- What are the restrictions on increment and decrement operators in java?
- Increment ++ and Decrement -- Operator Overloading in C++
- Pre-increment (or pre-decrement) in C
- Create increment decrement plus minus buttons programmatically for HTML input type number in JavaScript
- How and why to avoid global variables in JavaScript?
- Increment Negative and Decrement Positive Numbers by 1 in an Array in Java
- Write a C program to demonstrate post increment and pre increment operators

Advertisements