Increment and decrement Operators


Increment and decrement operator or operators are the most commonly used operators in Loops. Besides of any programming languages, increment and decrement operators are the very important concepts to understands but we will see it’s real time use in loops. JavaScript is also rich with these concepts and we will cover the usage concept of increment and decrement operators in javaScript.

What is an increment operator?


In JavaScript programming, increment operator is an arithmetic operation which is used to increase the value by 1. Let’s now jump into bit logical programming concepts instead of daily life so you can get into the depth and can understand the concept of increment operators.
Let suppose Alice is developing a software and while programming he was told to create a program that can show how many times a button was pressed. Alice got an idea that it could easily be done by creating a function and calling the function whenever the button is pressed. Now what to write in that function? Super simple, Alice would just using an increment operator within that function and increment operator will increase the value by 1.

Code
let x=0
x=x++ //increment operator is denoted by ++. I wanted to use increment operator on x so I used x here.
Consoile.log(x)

Output
1

What is a decrement operator?


Decrement operator has the same process of working like the increment operator in JavaScript programming language but with the different output performed. Unlike the increment operator, the decrement operator actually decreases the value by 1.
Same like the example mentioned in increment operator, Boss told Alice to create a program that can reduce the lives of the user whenever the user fails the attempts. For example, suppose the Boss wants Alice to develop a program that limits the user to try maximum three attempts. It means when the user hits the button the value should decrease by one.
Let’s code it
y=3
y=y– //Decrement operator is denoted by – with the variable to be used with
console.log(“Remaining lives are ”+y)

Output
Remaining lives are 2

What if I want to increase or decrease the value by the difference of more than 1?
This is very simple. The point where you used increment ( ++ ) or decrement operator ( — ). You can simply change it to the desired difference you want.
For example, I want to have the difference of 2 when the function is performed.


Code :
x=10
x=x+2 //Now increment operator would convert into arithmetic opration of addition
console.log(x)

Output
12

These are very easy concepts and are generally used in developing the systems. If you have any question please do ask us over the comments.

Buy on amazon with upto 40% off

RECOMMENDED FOR YOU

What is javascript?

Variables in javascript

let vs var vs const

Leave a Reply

Your email address will not be published. Required fields are marked *