1

Code

function hi() {
    myvar = 'local variable';
}
console.log(myvar);

Output: ReferenceError: myvar is not defined

According to my understanding, javascript goes line by line, examines the variables and their scope. Keeping that in mind shouldn't the following print 'undefined', as myvar exists in global scope?

6
  • 1
    Possible duplicate of Define global variable in a JavaScript function Commented Jun 13, 2018 at 10:22
  • 1
    @musefan it doesn't print undefined actually. The question is why is it not logging undefined (instead he has an error message) Commented Jun 13, 2018 at 10:22
  • Also be aware implicit global's will throw this error when using strict mode. Commented Jun 13, 2018 at 10:24
  • 2
    @musefan his question clearly states that he wants to understand why he received Output: ReferenceError: myvar is not defined as opposed to undefined Commented Jun 13, 2018 at 10:25
  • 1
    @musefan, when you're saying "It prints undefined because" that means you didn't try it out. The code doesn't print undefined at all Commented Jun 13, 2018 at 10:25

4 Answers 4

5

No it shouldn't, for it to print undefined you need to first define it in a global scope.

The fact that you are assigning some value to that variable inside of a function doesn't mean anything until the function itself is executed.

var myvar;

function hi() {
    myvar = 'local variable';
}
console.log(myvar);

If you omit that var myvar; line and and execute hi function before you do the console.log, you will get "local variable" as a result because that function creates an implicit global binding.

To check this, simply use strict mode and you will get error instead of the result above because strict mode prevents implicit globals.

"use strict";

function hi() {
    myvar = 'local variable';
}

hi();

console.log(myvar);

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

10 Comments

I think it is in global scope already. If you call function hi, before console.log(), it prints 'local variable'.
@PrabhatDoongarwal check now.
So, js first checks for function calls, before processing code in that function?
As long as you don't call hi js will not look at what's in the function's scope. If I'm not mistaken.
Are you sure or is it your hypothesis?
|
2

The problem: myvar throws a ReferenceError

function hi() {
  myvar = 'local variable';
}
console.log(myvar);

I understand you thought you would get undefined.

In the above code myvar hasn't been defined outside the function's scope. So when calling console.log(myvar) it will throw an error. You can ignore the function's declaration for this part, it's as if you had:

console.log(myvar)


Solution: set myvar outside of the function's scope

So you need to define your variable before logging it, for example:

var myvar;

function hi() {
  myvar = 'local variable';
}
console.log(myvar);

This will ensure that myvar is defined here

  • outside the function's scope: it doesn't have any value so it's undefined

  • in the function's scope: it is set to a string


More on global variables

As you haven't added the var operator before myvar = 'local variable'; you have essentially accessed the global variable myvar. So by calling hi() you will change the value of myvar outside of the function's scope:

var myvar;

function hi() {
  myvar = 'local variable';
}

console.log(myvar);
hi();
console.log(myvar);

6 Comments

myvar is in global scope, just call the function before console.log(), it prints 'local variable'.
Yes, did you three my third point on More on global variables?
Sorry, I saw it now. So, what is its value before I call hi()?
It's undefined as I've set the variable outside the function's scope with var myvar; on line 1.
Consider the code I mentioned in question. What is the value of myvar in that case?
|
2

As found in the MDN docs

You can declare a variable in three ways:

  • With the keyword var. For example, var x = 42. This syntax can be used to declare both local and global variables.

  • By simply assigning it a value. For example, x = 42. If this form is used outside of a function, it declares a global variable. It generates a strict JavaScript warning. You shouldn't use this variant.

  • With the keyword let. For example, let y = 13. This syntax can be used to declare a block-scope local variable. See Variable scope below.

Your function will work, because it satisfies method #2, declaring a variable by assigning it a value.

The console.log(myvar) will not work as myvar was never declared through any of the 3 means mentioned above. As such, you get a ReferenceError.

According to my understanding, javascript goes line by line, examines the variables and their scope. Keeping that in mind shouldn't the following print 'undefined', as myvar exists in global scope?

No, the function was not called, and as such the code inside it was not run, and so the variable was never created. If you had myvar = 'local variable'; outside of the function, it will work.

7 Comments

So, js checks for function call, before processing the code in a function?
Yes of course, it won't run functions for you unless you ask it to. Just to clarify I don't mean that if you call the function, the script will be parsed the script differently. The only difference is that by calling the function, the variable gets defined, as assigning a value to a variable also declares it (in javascript)
There is difference in executing and registering variables and their scope. Consider the following code. It prints undefined, even before the variable is declared.
But, I guess it could be different for functions.
No, you have added a new difference to the scenario called Variable Hoisting there which you are overlooking. Whilst it may appear that you have used a variable before it was defined, all var declarations are "hoisted" to the top of the scope by the JS parser. More info here: developer.mozilla.org/en-US/docs/Glossary/Hoisting
|
0

This is because the variable scope is local to the function. try declaring the variable outside first.

var myvar;
function hi() {
  myvar = 'local variable';
}
console.log(myvar);

2 Comments

I don't think it is local to the function. Because after you call the function then you are able to access it in the global scope. It just seems for some reason it doesn't set it to global scope until after the function call
@musefan, Indeed unless you use var the variable myvar will not be local to hi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.