++someVariable vs. someVariable++ in JavaScript

后端 未结 6 1941
长发绾君心
长发绾君心 2020-11-22 02:02

In JavaScript you can use ++ operator before (pre-increment) or after the variable name (post-increment). What, if any, are the differences b

6条回答
  •  后悔当初
    2020-11-22 02:42

    var x = 0, y = 0;
    
    //post-increment: i++ returns value then adds one to it
    console.log('x++ will log: ', x++); //0
    console.log('x after x++ : ', x);    //1
    
    //pre-increment: adds one to the value, then returns it
    console.log('++y will log: ', ++y); //1
    console.log('y after ++y : ', y);   //1
    

提交回复
热议问题