JavaScript Tips and Tricks: difference between VAR and LET variables Concept.

Difference between VAR and LET

As we know in order to declare a variable in JavaScript we have two options either declare with var or declare with let. Now the question is when to use var and when to use let. What are the major difference be…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Sayyed Asad Ullah

Difference between VAR and LET

As we know in order to declare a variable in JavaScript we have two options either declare with var or declare with let. Now the question is when to use var and when to use let. What are the major difference between both.

The main difference between let and var is that scope of a variable defined with let is limited to the block in which it is declared while variable declared with var has the global scope. So we can say that var is rather a keyword which defines a variable globally regardless of block scope.

The scope of let not only limited to the block in which it is defined but variable with let also do not get added with global window object even if it get declared outside of any block. But we can access variable with var from window object if it is defined globally.

Due to limited scope let variables are usually used when there is limited use of those variables such as in for loops, while loops or inside the scope of if conditions etc while var variable is used when value of variable need to be less change and used to accessed globally.

*Follow this code to check the differentiate between VAR and LET *

let a = 'hello'; // globally scoped
var b = 'world'; // globally scoped
console.log(window.a); // undefined
console.log(window.b); // 'world'
var a = 'hello';
var a = 'world'; // No problem, 'hello' is replaced.
let b = 'hello';
let b = 'world'; // SyntaxError: Identifier 'b' has already been declared


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Sayyed Asad Ullah


Print Share Comment Cite Upload Translate Updates
APA

Sayyed Asad Ullah | Sciencx (2022-09-13T11:49:52+00:00) JavaScript Tips and Tricks: difference between VAR and LET variables Concept.. Retrieved from https://www.scien.cx/2022/09/13/javascript-tips-and-tricks-difference-between-var-and-let-variables-concept/

MLA
" » JavaScript Tips and Tricks: difference between VAR and LET variables Concept.." Sayyed Asad Ullah | Sciencx - Tuesday September 13, 2022, https://www.scien.cx/2022/09/13/javascript-tips-and-tricks-difference-between-var-and-let-variables-concept/
HARVARD
Sayyed Asad Ullah | Sciencx Tuesday September 13, 2022 » JavaScript Tips and Tricks: difference between VAR and LET variables Concept.., viewed ,<https://www.scien.cx/2022/09/13/javascript-tips-and-tricks-difference-between-var-and-let-variables-concept/>
VANCOUVER
Sayyed Asad Ullah | Sciencx - » JavaScript Tips and Tricks: difference between VAR and LET variables Concept.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/13/javascript-tips-and-tricks-difference-between-var-and-let-variables-concept/
CHICAGO
" » JavaScript Tips and Tricks: difference between VAR and LET variables Concept.." Sayyed Asad Ullah | Sciencx - Accessed . https://www.scien.cx/2022/09/13/javascript-tips-and-tricks-difference-between-var-and-let-variables-concept/
IEEE
" » JavaScript Tips and Tricks: difference between VAR and LET variables Concept.." Sayyed Asad Ullah | Sciencx [Online]. Available: https://www.scien.cx/2022/09/13/javascript-tips-and-tricks-difference-between-var-and-let-variables-concept/. [Accessed: ]
rf:citation
» JavaScript Tips and Tricks: difference between VAR and LET variables Concept. | Sayyed Asad Ullah | Sciencx | https://www.scien.cx/2022/09/13/javascript-tips-and-tricks-difference-between-var-and-let-variables-concept/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.