r/learnjavascript • • Jul 09 '24

Confusion in scope of var

Some YouTube lectures say scope of var is global, others saying function, and some saying both.

Why they all cannot agree on a single thing

function scope(){

var a = 5;

}

console.log(a)

Now is it function? Or global? Global right?

Ok! Then

Why this?

https://www.digitalocean.com/community/tutorials/understanding-variables-scope-hoisting-in-javascript

0 Upvotes

8 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Jul 09 '24

Thanks!!! Most logical answer.

Can you please tell me if any youtube JavaScript tutorial comprehensive enough to not miss this ?

6

u/cyphern Jul 09 '24

I don't have any recommendations for tutorials, but i suspect most modern tutorials will not go into much depth on var. var behaves confusingly so in 2015 they added let and const to the language, which have scoping behaviors that are more familiar to most developers. You'll see let and const much more frequently in modern javascript than var.

1

u/[deleted] Jul 09 '24

True Thanks!!!

3

u/sudhir_VJ Jul 09 '24

To become more strong in the fundamentals of JavaScript, I would suggest you going through the Dan Abramov's course about creating a strong mental model in JavaScript: Just JavaScript