r/javascript • • May 28 '19

What are some JavaScript things beginners don't know?

333 Upvotes

383 comments sorted by

View all comments

141

u/inabahare May 28 '19

Using const and let

0

u/[deleted] May 28 '19

[deleted]

33

u/[deleted] May 28 '19

VSCode masterrace but WebStorm is right here. (Modern) JS shouldn't use var when not explicitly needed.

8

u/g0liadkin May 28 '19

I'm curious, would you say there is a situation when it's actually needed to use var?

-1

u/Aswole May 28 '19

One thing I've noticed when debugging from Chrome is that I can't access certain variables outside of a block unless I declared them with var (I'm guessing it has something to do with hoisting?). I still always use const/let, but there are times I will switch to var temporarily for that reason.

0

u/[deleted] May 28 '19

[deleted]

1

u/Aswole May 29 '19

I definitely didn't word that well. To illustrate:

const functionToDebug = params => {
  const myDog = 'Fido';
  var yourDog = 'Fido';
  const someClosure = () => {
    debugger
    //While debugging from browser, console.log(myDog) throws an error and will say myDog is not defined whereas console.log(yourDog) will work just fine and log 'Fido'
  };
};