Difference between let and var keyword in javascript - codewithrandom

Difference between let and var keyword in javascript - codewithrandom

Difference between let and var keyword in javascript - codewithrandom


Hey, learner Today we'll explore the Let and Var.

If you're a beginner in this field, then follow me because I'm going to explain it from scratch. 

So, Let and Var are keywords in Javascript with the help of we declare variables. Before moving ahead let's discuss what is variable for a short introduction to make the topic more clear for beginners. I know most of you have heard this name earlier. If you haven't, so don't worry we'll cover it up here. 

Variables

Variables are containers for storing data (values). In data, it can be number, name, boolean, string, object, etc.

In this below example, p, quantity, name, response are declared with the var keyword:

 var p = 15;  
 var quantity = 5;  
 var name = "Codewithrandom";  
 var response = 'Yes I am!';  
From the above example, you can expect:
  • p stores the value of 15.
  • quantity stores the value of 5.
  •  name stores the value of a string.
  •  response stores the value of a string
So by this what we are getting is that each variable is acting as a container that what I have mentioned in the definition.

We have some rules while declaring a JavaScript variable.  All Javascript variables must be a unique name.

  • A variable name must start with a letter (a to z or A to Z), underscore (_), or dollar( $ ) sign. 
  • A variable name cannot start with a number. After the first letter, we can use digits (0 to 9), for example, message1 and to23.
  • JavaScript variables are case-sensitive. For example, a and A are different variables.
  • Reserved words (like JavaScript keywords) cannot be used as names.

In JavaScript, there is three-way to declare variables.

  • With var
  • With let
  • With const

Var:-

Before the advent of ES6, var declarations were used to declare a variable. It was solely used to declare any variable. It has both local and global scope. (scope is just like the boundary of its effect).


Note:-  We have another article for Global and Local scope.

Declaring Javascript variables with the var keyword.

Variable declared using the var keyword followed by the name of the variable and semi-colon. Below is the syntax to create variables in JavaScript:

 var var_name;  
 var p;  
After the declaration, if we don't assign any value or data to the variable then (technically it carries the value of undefined).
 var p; // undefined  
 If you will run it then it prints the undefined value on the console.

To assign a value to the variable, we use the equal sign this is common in other programming languages:
 p = 15;  
 var milkName = "DMS";  
We can also assign a value to the variable at the time of declaration

Let's have a look glimpse of var.

  1. For declaring single and multi-variable we can use it.
 var person = "Rohan", profession = "programmer", Home = "USA";  
 console.log(profession);  
 console.log(person);  
 console.log(Home);  
 // it will print one by one each value which is collectively declare with var.  
     2.  For mathematical expression.
   
 var x = 5 + 20 + 1*3;  
 console.log(x); // output 28  
    3. Variable with re-declaration.
     
 var milkName = "MDS";  
 var milkName;  
 console.log(milkName);  

  If you re-declare a JavaScript variable, it will not lose its value.

Var as a Local variable.

Local Variable:-

Local variables are variables that are defined within functions. They have local scope, which means that they can only be used within the functions that define them.

 myfunction();  
 function myfunction() {  
   var routeName = "Sinifer 28"; // local variabl  
   console.log(typeof(routeName) + " " + routeName);  
   }  
 console.log(typeof(routeName));  
 //string Sinifer 28  
 //undefined  

The above example illustrates the use of a local variable. However, that a statement outside of the function can’t refer to the variable named routeName. So, it is showing an error of undefined it is so because it has local scope.

Var as a Global variable and Scope.

Global Variable:-

In contrast, global variables are variables that are defined outside of functions. These variables have global scope, so they can be used by any function.

1:  var routeName = "Sinifer 28"; //global variabl  
2:  myfunction();  
3:  function myfunction() {  
4:    console.log(typeof(routeName) + " "+"I have to go on this way" + routeName);  
5:    }  
6:  console.log(typeof(routeName)+ " "+ "I have to go on this way" + routeName);  
7:  //string I have to on this waySinifer 28  
8:  //string I have to on this waySinifer 28  

Let:-

Let is a keyword used to declare variables in javascript that is block-level scoped. Let is introduce in ES6(2015). The purpose of introducing let is to resolve all issues posed by variables scope, which developers face during development. The properties of let are that They have visibility linked to the block they belong with. We can change their values, but they cannot be redeclared in the same scope, unlike var.

Facts about Let keyword.

  • Variables defined with let cannot be Redeclared.
 let x = "box1";  
 let x = 13;  
 console.log(x);  
 //Identifier 'x' has already been declared  
  • Variables defined with let must be Declared before use
       
  •  For declaring single and multi-variable we can use it.
 let x=5, y=4, z=9;  
 console.log(x);  
 console.log(y);  
 console.log(z);  

Block-level Scope:-

Block Scope

Before ES6 (2015), JavaScript had only Global Scope and Function Scope. As I have mentioned that ES6 introduced two important new JavaScript: let and const. These keywords provide Block Scope in JavaScript.
Variable declare inside a {} block is termed as in block-level scope and it can't be accessed from outside the block.                         
 {  
   let id="MIB";  
   console.log(id);  
 }  
 console.log(id);  
 //MIB  
 //console.log(id);  
       ^  
 //ReferenceError: id is not defined  

Global Scope

 let num=101;  
 console.log(num);  
 function fun(){  
   console.log(num);  
 }  
 fun(); // calling the function  
 //101  
 //101  
Function Scope
 function add(){  
   let num=20;  
   console.log(num);  
 }  
 add(); // calling the function  
 console.log(num);  
 //20  
 //console.log(num);  
       ^  
 //ReferenceError: num is not defined  

written by codewave

Check out more.....

Post a Comment

Previous Post Next Post