Files
freeCodeCamp/guide/english/javascript/es6/let-and-const/index.md
Adewale Orotayo 26c4b20a9b Add "Second example of Const" to article (#22727)
* Add "Second example of Const" to article

Describing const also to be scoped i.e, accessible in the block where it was defined  can be added to the article

* Reordered changes and fixed formatting
2018-11-20 18:46:06 -05:00

71 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Let and Const
---
## Let
let is similar to var but let has scope. let is only accessible in the block level it is defined.
```
if (true) {
let a = 40;
console.log(a); //40
}
console.log(a); // undefined
```
In the above example variable a is defined inside an If statement and so its not accessible outside of the function.
Another example:
```
let a = 50;
let b = 100;
if (true) {
let a = 60;
var c = 10;
console.log(a/c); // 6
console.log(b/c); // 10
}
console.log(c); // 10
console.log(a); // 50
```
## Const
Const is used to assign a constant value to the variable, and the value cannot be changed. It is fixed.
```
const a = 50;
a = 60; // shows error. You cannot change the value of const.
const b = "Constant variable";
b = "Assigning new value"; // shows error.
```
Like `let`, `const` is also block scoped, i.e, only accessible in the block it is defined in.
Consider this second example.
```
const a = 15;
const b = 20;
if (true) {
const a = 20;
const c = 4;
console.log(a/c); // 5 Here, a is the block variable which has the value 20
console.log(b/c); // 5 Here, b is the outer variable with value 20
}
console.log(a); // 15
console.log(c); // ReferenceError: c is not defined
```
Consider another example.
```
const LANGUAGES = ['Js', 'Ruby', 'Python', 'Go'];
LANGUAGES = "Javascript"; // shows error.
LANGUAGES.push('Java'); // Works fine.
console.log(LANGUAGES); // ['Js', 'Ruby', 'Python', 'Go', 'Java']
```
This may be little confusing.
Consider in this way. Whenever you define a const variable, Javascript references the address of the value to the variable. In our example the variable LANGUAGES actually references to the memory allocated to the array. So you cannot change the variable to reference some other memory location later. Throughout the program it only references to the array.