Skip to content
Advertisement

How the output of the below code becomes 1 need good explanation

The result of the above program is 1 but I am not sure how its giving the result of 1. someone pls explain this to me.. it was asked in one of the interview I had for automation

JavaScript

Advertisement

Answer

So at first break this statement and try to understand what’s going on

JavaScript

So you must be aware with operator precedence

so here what’s going on is

at first a– is getting evaluated as it is postfix decrement

Postfix decrement operator means the expression is evaluated first using the original value of the variable and then the variable is decremented(decreased).

so the expression is becoming like this

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement