JavaScript Increment & Decrement Input Output Questions & What is x++ & var x = x++ ???
JavaScript Increment & Decrement Operator
var x = 10; var x = x++; var x = ++x; console.log(x); *output* 11
var x = 10; var x; var x = ++x; console.log(x); *output* 11 (If you are not clear, dont worry, we will explain this in this post as well)
var x = 10; var y = x++; var z = ++x; console.log(x,y,z); 12 10 12
var x = 10; var x = undefined; var x = ++x; console.log(x); NaN
var x = 10; var x = ++x; var x = x++; console.log(x); 11
var x = 10; var x = ++x; var x = x++; var y =x; console.log(x); console.log(y); 11 11
var x = 99; x = x++; console.log( x ); 99
var x = 10; var x = ++x; var x = x++; var x = x++; var x = x++; var y =x; console.log(x); 11
var x = 10; var x = ++x; var x = x++; var x = ++x; var x = x++; var y =x; console.log(x, y); 12 12
— — -I am sure that you all will be confused on above questions and it’s output…
Confusion !!!!!
Let’s explain it in a simple way.
You probably learned that x++ is the same as x = x+1. Well, that’s not the whole story. x = x++ and simple x++ both are not same.
When we do var x = x++; it simply means we are doing var x = x nothing else. you’ve just set x back to its old value: !! It is not same as var x = x + 1;
But
When we do x++ only: it is same as x = x + 1;
Example :
var x = 10; var x = x++; console.log(x); the value of x is still > 10;
var x = 12; x++; console.log(x); // Now the value of x is 13.
Another important question. what will be the output for the below code?
var x = 0; var loop = function(){ while(x < 3){ console.log(“I’m looping!”); x= x++; } }; loop();
Think! Think !! Think !!
— — — — — –
— — –
— –
—
— –
And the answer is it will run as an infinite loop as per the above explanation.
Read more technical blogs – https://jsmount.com
Read here HTML interview questions answers
https://www.jsmount.com/html-advance-interview-questions-and-answers-for-experienced-part-1/
Top Interview Questions and Answer of HTML 5 Canvas
How to draw Canvas Image, Line & Circle With HTML & JavaScript
Angular Create a Custom directive to match password and confirm password
Tips to Boost Angular App Performance | Web page Speed Optimization
JavaScript Rotate 2D matrix 90 degrees clockwise | Top Interview Question
HashTable Data Structure in JavaScript with Add Delete & Search Algorithms
Trie Data Structure – Insert Search Delete & Print Program with JavaScript
Top JavaScript Commonly asked Algorithms in Interview
JavaScript String Permutation Program with nice explanation
TypeScript New & Latest Features You should know | JS Mount
Rx JS Top Operators with examples | Rx JS interview questions
JavaScript Increment & Decrement Operator.