r/javascript • • May 28 '19

What are some JavaScript things beginners don't know?

333 Upvotes

383 comments sorted by

View all comments

Show parent comments

8

u/g0liadkin May 28 '19

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

10

u/Careerier May 28 '19

When you can't use a compiler like Babel and you have to support older browsers that don't know what `let` and `const` mean.

5

u/[deleted] May 28 '19

I imagine it can be more convinent sometimes to have a function-scoped variable. But even then we're probably talking about quickly hacking things together.

Out of the blue I cannot give you a production example of "a var variable would be better here"

8

u/turntopage294 May 28 '19

Out of the blue I cannot give you a production example of "a var variable would be better here"

I couldn't even give you a textbook example...

1

u/HansonWK May 28 '19

When writing rules in Google Tag Manager... Something I wish I could avoid completely but sadly cannot.

1

u/[deleted] May 28 '19

My main js code at work is used in an old Rhino engine so I normally have to use var at work.

-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.

15

u/ergnui34tj8934t0 May 28 '19

That's in the definition of const and let...

1

u/Aswole May 29 '19

(Copied from another reply) 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'
  };
};

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'
  };
};

3

u/aust1nz May 28 '19

If you declare those variables using let or const first outside of the block, and then assign values to them within your block, they will be accessible in the outer scope without becoming globally assigned like with var.

4

u/DougWebbNJ May 28 '19

If "outside of the block" is the same as "at the top-level of the function's block", then let, const, and var are equivalent. That's the behavior I assumed Aswole was trying to achieve, by switching const/let to var without moving the variable definition.

1

u/aust1nz May 28 '19

Thanks for clarifying! You're right, and I think my reply was unclear -- you can declare a variable using let at the top of a function and reassign it within a block, if you want to completely avoid using var.

1

u/Aswole May 29 '19

You correctly interpreted my poorly worded comment.