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

5
  • Does this answer your question? What's the difference between using "let" and "var"? Commented Mar 10, 2020 at 1:41
  • 1
    Without the corresponding variable declaration, it gets declared as a property on the global object. Which is window in a browser. Commented Mar 10, 2020 at 1: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. Commented Mar 10, 2020 at 1: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"? Commented Mar 10, 2020 at 1:45
  • @RobG: Updated, thanks. Not sure where that terminology came from actually, I was probably mixing it up with something else. Commented Mar 10, 2020 at 1:46

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.