Monday 23 September 2019

Can let replace var in JavaScript?




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?


Answer



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.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...