Increment and decrement Operators
What is an increment operator?
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.
x=x++ //increment operator is denoted by ++. I wanted to use increment operator on x so I used x here.
What is a decrement operator?
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=y– //Decrement operator is denoted by – with the variable to be used with
console.log(“Remaining lives are ”+y)
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.
x=x+2 //Now increment operator would convert into arithmetic opration of addition
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