JavaScript ++ Operator | X++ and X = X++ Explanation
JavaScript ++ operator, javascript increment operator, increment operator in javascript, increment and decrement operators in javascript with examples, javascript decrement operator, JavaScript ++, javascript 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
- Implementation of Queue using Linked List | JavaScript
- Insert node in Linked list | Algorithm | JavaScript
- Insertion Sort in data structure | Algorithm with Examples
- Selection Sort Algorithm & K’th Largest Element in Array
- Quick Sort Algorithm with example | Step-by-Step Guide
- Dependency Inversion Principle with Example | Solid Principles
- Object-Oriented Programming | Solid Principles with Examples
- ASCII Code of Characters | String Operations with ASCII Code
- Negative Binary Numbers & 2’s Complement | Easy explanation
- Factors of a Number | JavaScript Program | Optimized Way
- LeetCode – Game of Life Problem | Solution with JavaScript
- Fibonacci series using Recursion | While loop | ES6 Generator
- JavaScript Coding Interview Question & Answers
- LeetCode – Coin Change Problem | Dynamic Programming | JavaScript
- HackerRank Dictionaries and Maps Problem | Solution with JavaScript
- React Redux Unit Testing of Actions, Reducers, Middleware & Store
- Micro frontends with Module Federation in React Application
- React Interview Question & Answers – Mastering React
- Top React Interview Question & Answer | React Routing
- React Interview Questions and Answers | React hooks
- Higher Order Component with Functional Component | React JS
- Top React Interview Questions and Answers | Must Know
- Interview Question React | Basics for Freshers and Seniors
- Cyber Security Fundamental Questions & Answers You must know
- Application & Web Security Interview Questions & Answers
- Top Scrum Master and Agile Question & Answers 2022
- Trapping Rain Water Leetcode Problem Solution
- Array Representation of Binary Tree | Full Tree & Complete Binary Tree
- Graphs in Data Structure, Types & Traversal with BFS and DFS, Algorithms
- Traversing 2 D array with BFS & DFS Algorithm in JavaScript
- Time Complexity & Calculations | All You should know
- Backspace String Compare Leetcode Problem & Solution
- Angular Interview Questions & Answers 2021 – Part 3 – JS Mount
- Why Angular is a Preferred Choice for Developers? Top Features
- Angular Interview Questions & Answers You should know Part 2
- Top 30 JavaScript Interview Questions and Answers for 2021
- React JS Stripe Payment Gateway Integration with Node | Step by Step Guide
- Create Year Month & Date dropdown List using JavaScript & JQuery
- Create Custom QR Code Component using QR Code Styling in React JS
- How to create a common Helper class or util file in React JS
- React Build Routing with Fixed Header and Navigation | React Router Tutorial
- React Create Dashboard Layout with Side Menu, Header & Content Area
- Web Application Security Best Practices | Top Tips to Secure Angular App
- HTML Form with Pure CSS & JavaScript | Custom Radio and Checkbox
- NgRx Top Interview Questions and Answers You should know
- Top 40 Awesome CSS Interview Questions & Answers You should know | CSS Tutorial
- 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
- Angular NgModule, AOT, and DI, and EntryComponents with example
- Angular Dependency Injection Provider and Types of Provider
- TypeScript New & Latest Features You should know | JS Mount
- Rx JS Top Operators with examples | Rx JS interview questions
- What’s new in Angular 9 | Top updates in Angular 9 By JS mount
- What’s new in Angular 8 | Angular 8 Latest Feature By JS mount
- Prime NG Row Group with Subheader and Totals Row
- How to implement Reactive Form with Prime NG table
- Angular Unit Testing Spy, Router, Fake Async, tick, & Subscribe
- Angular Auto Tab Directive : Auto focus on next field
- VS Code Useful Extensions for Web Development
- Angular HTTP Request Testing with HttpClientTestingModule & HttpTestingController
- Dynamic Tooltip with Angular Pipe: Performance Orientation
- Konva JS Event Handling and Get Overlapped Shapes where clicked
- JavaScript Top Interview questions & Answers You should know
- Best Practices for Writing Angular Apps | Angular Guidelines
- Top 30 TypeScript Interview Questions & Answers You Must Know
- HR Interview Questions and Answers for Experienced
- Mastering JavaScript Interview Questions & Answers
- Commonly Asked Coding Interview Questions JavaScript
- Angular Interview Question & Answers for Experienced – Part 1
- Most Asked JavaScript Interview Questions for Experienced
- Frequently Asked HTML Interview Questions and Answers
- How to upload Image in Node JS & Handle file size, file type error with Multer
- Angular Custom directive to match password and confirm password
- Draw Canvas Image, Line & Circle With HTML & JavaScript
- HTML Interview Questions and Answers on Canvas
- JavaScript Console Interview Questions | Input Output Program
javascript increment operator, increment operator in javascript, increment and decrement operators in javascript with examples, javascript decrement operator, JavaScript ++, javascript operator