let, const, and var in JavaScript
By: Stella in Javascript Tutorials on 2023-04-25
In JavaScript, let
, const
, and var
are used for declaring variables, but they have different scopes and behaviors. Here are the main differences:
var
: This keyword has function scope, which means that the variable declared withvar
can be accessed within the function in which it was defined. If a variable is declared withvar
outside of any function, it will be a global variable and can be accessed throughout the code.var
can be re-declared and updated.
Example:
function foo() { var x = 5; if (true) { var x = 10; console.log(x); // Output: 10 } console.log(x); // Output: 10 }
let
: This keyword has block scope, which means that the variable declared withlet
can be accessed only within the block in which it was defined (for example, within anif
statement,for
loop, or function).let
can be updated but not re-declared.
Example:
function foo() { let x = 5; if (true) { let x = 10; console.log(x); // Output: 10 } console.log(x); // Output: 5 }
const
: This keyword also has block scope, but it is used for declaring constants. The value of aconst
variable cannot be changed or re-declared.
Example:
function foo() { const x = 5; if (true) { const x = 10; console.log(x); // Output: 10 } console.log(x); // Output: 5 }
In general, it is recommended to use const
for values that will not be changed, let
for values that will be changed, and var
for global variables. However, the choice of which keyword to use ultimately depends on the specific needs of the code and the programmer's preference.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comments (1)
var declarations can have surprising behaviors (for example, they are not block-scoped), and they are discouraged in modern JavaScript code.
If you declare a variable without assigning any value to it, its value is undefined. You can't declare a const variable without an initializer, because you can't change it later anyway.