From ebb515e0b8799df86392a7519d076dee55ab0a78 Mon Sep 17 00:00:00 2001 From: MANJEET KUMAR <43195111+Manjeete@users.noreply.github.com> Date: Sat, 12 Oct 2019 01:49:16 +0530 Subject: [PATCH] Define the ES6 variables ES6 variables : var ,let and const are defined with the help of example. --- basics/variables.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/basics/variables.md b/basics/variables.md index 91f2cca7..038b2e86 100644 --- a/basics/variables.md +++ b/basics/variables.md @@ -48,3 +48,43 @@ var x = 5; var y = 6; var result = x + y; ``` +## ES6 version +Now jump to ES6 version of javaScrip. +In ES6 we have three ways of declaring variables + +``` +var x = 5; +const y = 'Test'; +let z = true; +``` +The types of declaration depends upon the scope. +Unlike the "var" keyword , which defines a variable globally or locally to an entire function regardless of block scope, "let" allows you to declare variables that are limited in scope to the block , statement or expression in which they are used. +FOR Example: +``` +function varTest(){ + var x=1; + if(true){ + var x=2; // same variable + console.log(x); //2 + } + console.log(x); //2 +} +``` +``` +function letTest(){ + let x=1; + if(true){ + let x=2; + console.log(x); // 2 + } + console.log(x); // 1 +} +``` +Now the third variable type is "const". +const variables are immutable - they are not allowed to re-assigned. +For Example: +``` +const x = "hi!"; +x = "bye"; // this will occurs an error +``` +