Learn to Code via Tutorials on Repl.it!

← Back to all posts
[JavaScript] What is the diffrence of var and let?
zplusfour

Hi

Some people ask a lot, is there a diffrence between the var keyword and the let keyword? Well, yes, there is.


Explaination

We use let and var to declare variables, to use them with our program.

For example:

But then you'll tell me: "If we declare variables with both, why there is a diffrence?".
Ok, have an example:

Why s is undefined? Well because we use let to declare variables in brackets or we can just call them local variables.
You can't use it outside the brackets that it is declared in.

But with var:

With var, you declare global variables which you can use it anywhere (of course not outside a function or a class).

Closing

I hope you enjoyed this tutorial, have a great day!

  • ZDev1
Voters
thelonecodist
programmeruser
SixBeeps
zplusfour
Comments
hotnewtop
thelonecodist

This is a helpful post because it reminded me about how var works. I've been only using let and const for a while, so I needed a quick refresher. Thanks!

zplusfour

@thelonecodist thanks and np! :D

SixBeeps

Nice and to the point

zplusfour

@SixBeeps thanks