Var VS Let in Javascript

In Javascript, you can declare a variable using “Var” or “let” BUT there is an inconvenient, don’t worry, I will give you context.
Some years ago(2015) Javascript changed. Var had a global scope before 2015, BUT with ecmascript6 it changed because We c…


This content originally appeared on DEV Community and was authored by Ricardo Garrido

In Javascript, you can declare a variable using “Var” or “let” BUT there is an inconvenient, don’t worry, I will give you context.
Some years ago(2015) Javascript changed. Var had a global scope before 2015, BUT with ecmascript6 it changed because We can use “Let” to declare a variable.
You shouldn’t use “Var” because it is considered a bad practice , It can get you into a value reassignment problem.
Example:
Image description

In this example I created a variable using “var” and printed on console three times to show you this…
Image description

You can see that the variable var was added to window’s global object.
Image description

Image description

BUT if you create a variable using LET, you won’t have that problem, and the variable LET could be a local or global variable.
Image description

I hope I have helped you by solving this question and You have saved time.
I look forward to your comments, have a nice day. =D


This content originally appeared on DEV Community and was authored by Ricardo Garrido


Print Share Comment Cite Upload Translate Updates
APA

Ricardo Garrido | Sciencx (2021-10-25T22:24:11+00:00) Var VS Let in Javascript. Retrieved from https://www.scien.cx/2021/10/25/var-vs-let-in-javascript/

MLA
" » Var VS Let in Javascript." Ricardo Garrido | Sciencx - Monday October 25, 2021, https://www.scien.cx/2021/10/25/var-vs-let-in-javascript/
HARVARD
Ricardo Garrido | Sciencx Monday October 25, 2021 » Var VS Let in Javascript., viewed ,<https://www.scien.cx/2021/10/25/var-vs-let-in-javascript/>
VANCOUVER
Ricardo Garrido | Sciencx - » Var VS Let in Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/25/var-vs-let-in-javascript/
CHICAGO
" » Var VS Let in Javascript." Ricardo Garrido | Sciencx - Accessed . https://www.scien.cx/2021/10/25/var-vs-let-in-javascript/
IEEE
" » Var VS Let in Javascript." Ricardo Garrido | Sciencx [Online]. Available: https://www.scien.cx/2021/10/25/var-vs-let-in-javascript/. [Accessed: ]
rf:citation
» Var VS Let in Javascript | Ricardo Garrido | Sciencx | https://www.scien.cx/2021/10/25/var-vs-let-in-javascript/ |

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.