Skip to main content

Posts

Let vs Var vs Const in JavaScript let vs var vs const are the things that always keeps us in amusement that on what time which one to use. So here in this post all your doubt’s will get clear on where you need to use any of these and what are their advantages. As per the ES6 which came in 2015 there are new ways to create a variable in JavaScript with “let” and “const”. Moreover, to understand this we will firstly go through topics like function vs block scope, variable hoisting, and immutability. Variable Declaration vs Initialization var x Above we create a new identifier called ‘x’. In JavaScript, variables are by default initialized with the value of undefined whenever they are created. So, if we try to log the ‘x’variable, we’ll get undefined. var x console.log (x) // var x is undefined In contrast to variable declaration, variable initialization is done whenever you assign a value to a variable either at the time of declaration or after that. var x; ...