Increment and Decrement with JavaScript

Explain about increment and decrement operator…

Solve this_

                1.    var myVar=32;
                       var newVar;
                       console.log(myVar++);// how the output is 32 why the not increasing..

                2. var alfa=10,bita;
                    console.log(alfa++,alfa++);//what the output and how plzz give response....

Basically, increment increases the value of a variable by one. Decrement decreases by one.

var a = 5;
console.log(a); // 6

var b = 8;
console.log(b); // 7

reply me for my updated vary question

Now we go out of the basics of incrementing. When you put myVar++ as part of a bigger expression (like in console.log(myVar++)), two things happen in order:

  1. The current value of myVar is used. That’s why you see 32 in the output.
  2. The value is increased by 1.

After the console.log, myVar's value is now 33.

Why don’t you run it and see for yourself?

There is another variation of the increment operator: placing the ++ in front of the variable (like in ++alfa). When used as its own statement (aka the basic usage), it’s no different from alfa++. But when part of a larger expression, the value is incremented first, then the updated value is used.

bita = ++alfa; // bita will be 11 because alfa is incremented first.

As for the last line, it’s the same reasoning as the first example. Since by the time the code hits the console.log line, alfa's value is now 11. It will first print 11 then increment, then print 12 and increment again.

Tip. Next time, make a new thread to ask a new question, instead of editing an existing one. Your edit might go unnoticed for a long time and you can’t really expect it to be noticed by other users. Just don’t make multiple threads for the exact same question :wink: