Skip to main content

Posts

Showing posts with the label JavaScript Let vs Var

JavaScript Let vs Var - When should let be used over var?

Let Keyword - The “ let ” keyword declares a block-scoped variable. Let and var declared outside any block, both are global. The “ let ” keyword allows you to declare a variable that are limited in scope to the block, statement, or expression on which it’s used. You can assign values to the variables when you declare them or later in your script. If you do not initialize your variable in the “ let ” statement, it is automatically assigned the JavaScript value. Let variables cannot be re-declared while var variable can be re-declared in the same scope. A variable declared by “ let ” can’t be used before its declaration or an error will result. A variable declaration with “ let ” is helpful when we want the scope of a variable to be limited to statement or expression level. Let gives you the privilege to declare variables that are limited in scope to the block, statement of expression unlike var. The “let” statement declares a local variable in a block sco...