16

I'm from a Java background and started learning JavaScript.

Declaring variables in JavaScript using the keyword let sounds like is uses similar scope rules of declaring variables in Java. The concept of hoisting in JavaScript is confusing (consider my C++/Java background) and I don't see any pitfalls in using let in place of var.

Is my understanding correct?

danwellman
  • 9,068
  • 8
  • 60
  • 88
FullMoon
  • 724
  • 6
  • 20

2 Answers2

11

Yes, your understanding is correct.

Some experts even recommend solely using let everywhere, if it is available in your environment (Douglas Crockford said it in his Pluralsight course JavaScript the Good Parts).

ESLint even has a rule not to use var in ES6 Environments.

Domysee
  • 12,718
  • 10
  • 53
  • 84
  • 2
    Ohh how painfully wrong this answer is. Let should be limited inside of scopes and areas where it needs to be. Const and let are not replacements of let and it is wrong and a band wagon to believe so. I highly recommend watching this: https://www.lynda.com/JavaScript-tutorials/Let-vs-var/604265/618412-4.html – TheBlackBenzKid May 03 '18 at 13:41
  • 1
    i support the point made by @TheBlackBenzKid. let is *not* the new var. – certainlyakey Aug 08 '18 at 13:50
1

Correct. However, assuming you're developing for the web; Be sure to read browser compatibility first: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Browser_compatibility

Arman
  • 88
  • 1
  • 6
  • Well, I'm learning Java Script as i'll have to (in fact started working on server side) use nodejs. – FullMoon Sep 26 '15 at 10:57