Languages
[Edit]
EN

JavaScript - how to use let keyword

1 points
Created by:
Eshaal-Wilkinson
774

In this article, we would like to show you how to use the let keyword in JavaScript.

Introduction

Edit

The let keyword has been introduced to JavaScript in ES2015. There are a few important things that should know for each programmer who wants to use let keyword.

1. let keyword syntax example

Edit

let syntax is similar to var

  1. variable declaration
  2. variable declaration with value assigning
  3. if the variable is not assigned

    Output:

  4. multiple variables declaration with one let keyword

    Output:

  5. multiple variables declaration with value assigning

    Output:

     

  6. multiple variables declaration, some with value assigning

    Output:

2. let keyword scope example

Edit

The scope of let is the nearest enclosing curly brackets.

Output:

3. let keyword with for loop

Edit

When let keyword is used with for loop the scope of variable is for whole for statement.

Output:

4. Difference between let and var keywords

Edit

The difference has been described here.

5. Tips

Edit
  • ALWAYS use let keyword.

See also

Edit
  1. JavaScript - ECMAScript / ES versions and features

Alternative titles

  1. How to use let in JavaScript?
  2. JavaScript - How to use let?
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join