JAVASCRIPT Tutorial

let in JS

Key Concepts:

  • Block Scope: Variables declared with 'let' are accessible only within the block where they are declared.
  • No Re-declaration: 'let' variables cannot be re-declared within the same scope.
  • Re-assignment: 'let' variables can be re-assigned with new values.

Practical Steps:

  1. Declare the variable using 'let': Use 'let' followed by the variable name.
  2. Assign a value to the variable: Include the assignment operator '=' and the value you want to assign.
  3. Access the variable: You can access the variable within the same block.

Example:

let num; // Declares 'num' as a 'let' variable
num = 10; // Assigns the value 10 to 'num'
console.log(num); // Logs the value of 'num'

Additional Notes:

  • 'let' variables are initialized with the value 'undefined' if no initial value is assigned.
  • 'let' is introduced in ES6 (ECMAScript 2015) and is not supported in older JavaScript versions.
  • Use 'let' for variables that need to have a limited scope or that you may want to re-assign within a block.