When should I use let and var? [closed]

I would say that you should, as a principle, use let whenever it is not inconvenient to do so. Such as:

for (let i = 0; i < 100; i++) {
    // Do something
}

if (condition) {
    let msg = a + b + c;
    console.log(msg);
    alert(msg);
}

The advantages to this approach is:

  1. Less risk of overriding some global variable use for something else
  2. Less risk of memory leaks due to variables staying in memory long after they have become irrelevant

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)