-3

What is the scope of the default variable in javascript? I'm confused, I would like to know what Alcanse has the default variables I think is var or not?

For example:

x = 15 // ??
let y = 22 // let
var j = 33 // var

var? let?

Devansh Baldwa
  • 129
  • 2
  • 4
  • 14
  • Does this answer your question? [What's the difference between using "let" and "var"?](https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var) – ItsPete Mar 10 '20 at 01:41
  • 1
    Without the corresponding variable declaration, it gets declared as a property on the global object. Which is `window` in a browser. – David Mar 10 '20 at 01:42
  • 1
    What is "default variable"? If you mean an undeclared variable, the scope is global but it only comes into existence when the expression assigning to it is executed. In strict mode it's a syntax error. – RobG Mar 10 '20 at 01:42
  • 1
    @David—it's always on the global object (where window is an alias/synonym for the global object in environments that have window). What is "currently scoped object"? – RobG Mar 10 '20 at 01:45
  • @RobG: Updated, thanks. Not sure where that terminology came from actually, I was probably mixing it up with something else. – David Mar 10 '20 at 01:46

1 Answers1

0

Based on this source

Scope of the variables declared without var keyword become global irrespective of where it is declared. Global variables can be accessed from anywhere in the web page. Visit Scope for more information.

  • 1
    The OP is about undeclared variables, such as those created by assignment. The linked article uses incorrect terminology to the point of being misleading: "variables declared without var [or let or const] keyword become global irrespective of where it is declared". The point is that they aren't declared at all, they are created by assignment and have other important features besides being global after they are created. – RobG Mar 10 '20 at 03:26