

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Python Increment and Decrement Operators
- Increment and decrement operators in Java
- Increment and Decrement Operators in C#
- Increment ++ and decrement -- Operators in C++
- Increment and Decrement Operators in Python?
- 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
- How and why to avoid global variables in JavaScript?
- Write a C program to demonstrate post increment and pre increment operators
- Create increment decrement plus minus buttons programmatically for HTML input type number in JavaScript
- Why should we not use ++, -- operators in JavaScript?
Advertisements