CodesJava

Easy learning with example program codes

Difference between var and let


var keyword:

The var statement is used to declare a variable. We can optionally initialize the value of that variable.
Example: var num =10;

let keyword:

The let statement is used to declare a local variable in a block scope. It is similar to var, in that we can optionally initialize the variable.
Example: let num =10;

Scope difference between var and let:

Var scope:
The var is scoped to the nearest function block. In the below example variable num2 is declared within a block but can be accessed within the function and outside this block. The statement in which we are using this variable should be after this block which means we cannot access num2 before the declaration statement.

function varTest(){
  var num1 =10;	    
  console.log(num1);  //output 10
  if(true){
   var num2=20;       
   console.log(num2); //output 20
  }
  console.log(num1);  //output 10
  console.log(num2);  //output 20
}

Let scope:
The let is scoped to the nearest enclosing block. In the below example variable num2 is declared within a block and cannot be accessed outside this block.

function letTest(){
  var num1 =10;	    
  console.log(num1);  //output 10
  if(true){
   var num2=20;       
   console.log(num2); //output 20
  }
  console.log(num1);  //output 10
  console.log(num2);  //Cannot be accessed here
}

Redeclaration difference between var and let:

Redeclaration with var:
The var keyword provides the facility to re-declare the same variable in the same scope.

function varTest(){
  var num1 = 10;	    
  var num1 = 20;  //num1 is replaced
}

Redeclaration with let:
The let keyword does not provides the facility to re-declare the same variable in the same scope.

function letTest(){
  var num1 = 10;	    
  var num1 = 20;  // SyntaxError: Identifier num1 has already been declared
}


TypeScript Tutorial

TypeScript Overview.
Typescript vs Javascript.
Typescript let vs var.
TypeScript Hello World.
Typescript Data Types.
Typescript Variable Declaration.
TypeScript operators list.
TypeScript Arithmetic Operators.
TypeScript Comparison Operators.
TypeScript Bitwise Operators.
Typescript Logical Operators.
TypeScript Assignment Operators.
Typescript Conditional Operator.
Typescript String Operator.
Typescript typeof Operator.
TypeScript control statements.
TypeScript Switch Statement.
TypeScript for loop.
TypeScript for in loop.
TypeScript while loop.
TypeScript do while loop.
Typescript for loop break.
TypeScript for loop continue.
Typescript function.
Typescript returning function.
Typescript parameterized function.
Typescript function optional parameter.
Typescript rest parameters.
Typescript default parameters.
Typescript anonymous function.
Typescript function constructor.
Typescript Arrow/Lambda function.
Typescript function overloading.
TypeScript number object.
TypeScript string object.
TypeScript boolean object.
TypeScript date object.
TypeScript math object.
TypeScript array object.
TypeScript tuples.
TypeScript union type.
TypeScript interface.
TypeScript interface inheritance.
TypeScript interface multiple inheritance.
TypeScript class.
Typescript class inheritance.
Typescript class interface.
TypeScript abstract class.
TypeScript object.
TypeScript duck typing.
TypeScript NameSpace.
TypeScript Multi-file namespaces.
TypeScript module.
TypeScript ambient module.
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki