JavaScript let

This keyword is used to declare variables locally. If you use this keyword to declare a variable then the variable can be accessible locally and it is changeable as well. It is good if the code gets huge.

Syntax:

let variableName = "Variable-Value;"

Example: This example shows the use of let.

javascript
if (true) {
    let geeks = "w3wiki";
    console.log(geeks);
}

/* This will be error and 
   show geeks is not defined */
console.log(geeks);

Output
w3wiki

How to declare variables in different ways in JavaScript?

In JavaScript, variables can be declared using keywords like var, let, or const, each keyword is used in some specific conditions. Understanding these declarations is crucial for managing variable lifetimes and avoiding unintended side effects in code.

Table of Content

  • JavaScript var
  • JavaScript let
  • JavaScript const
  •  Difference Between var, let, and const

Similar Reads

JavaScript var

This keyword is used to declare variables globally. If you use this keyword to declare a variable then the variable can be accessible globally and changeable also. It is good for a short length of codes, if the codes get huge then you will get confused....

JavaScript let

This keyword is used to declare variables locally. If you use this keyword to declare a variable then the variable can be accessible locally and it is changeable as well. It is good if the code gets huge....

JavaScript const

This keyword is used to declare variable locally. If you use this keyword to declare a variable then the variable will only be accessible within that block similar to the variable defined by using let and difference between let and const is that the variables declared using const values can’t be reassigned. So we should assign the value while declaring the variable....

Difference Between var, let, and const

JavaScript var JavaScript let JavaScript const Can be redeclaredCannot be redeclaredCannot be redeclaredCan be reassigned a valueCan be reassigned a valueCannot reassign the valueOnly have global and function scopeCan have a block scopeCan have a block scopeVariables are hoisted on top and can be used anywhereVariables must be initialized before useVariables must be initialized before useCan be redeclared anywhere in the programCan be redeclared inside a blockCan never be redeclared...