From 26c4b20a9b6513e2787943492c07fbc0b01e5be7 Mon Sep 17 00:00:00 2001 From: Adewale Orotayo Date: Wed, 21 Nov 2018 00:46:06 +0100 Subject: [PATCH] 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 --- .../javascript/es6/let-and-const/index.md | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/guide/english/javascript/es6/let-and-const/index.md b/guide/english/javascript/es6/let-and-const/index.md index 1a9221d6dd6..b454aef02b1 100644 --- a/guide/english/javascript/es6/let-and-const/index.md +++ b/guide/english/javascript/es6/let-and-const/index.md @@ -31,13 +31,31 @@ console.log(a); // 50 ## Const -Const is used to assign a constant value to the variable. And the value cannot be changed. It's fixed. +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. ```