Global scope and functions, trying to replicate results

Tell us what’s happening:
trying to replicate the effects in other places, like visual studio, playcode and the live server add on in visual studio, but only get the result for the global variable made outside the function. The hint video said something about the code working differently depending on where it is used, so the scope for the variable not declared with var is local and not global. what should I be using so that the variable without var is global?

Your code so far

var outBlock = 1;

function fun1() {
    inBlockNoVar = 2;
    var inBlockWithVar = 3;

console.log(outBlock); //1
console.log(inBlockNoVar); // inBlockNoVar is not defined
console.log(inBlockWithVar); //inBlockWithVar is not defined

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36.

Challenge: Global Scope and Functions

Link to the challenge:

whenever you use a variable for the first time you MUST declare it first with var,let or const.

variables inside functions have ‘scope’ (can be seen) only from inside that function. think of global scope as anything inside that file.

You have to invoke the function before console logging the values :wink:

Not really. I’d say you SHOULD, but it’s no obligatory

In addition to the comments above, don’t use ever use var, but let or const. The only reason why var is still in the language is because if it was removed, the internet would explode. And all nuclear reactors that are running with JavaScript, of which I hope there are not many.

var wasnt removed because lots of codebases still rely on older keywords of js and removing it would break their websites. doesnt really matter if you still use var is just makes it easier to write bugs in your code if you do

hmm i didnt know that. figured the linter would have a stroke

@alkapwn3d If you use a linter :smiley:

got it, here is the result

let outBlock = 1;

function fun1() {

inBlockNoVar = 2;

let inBlockWithVar = 3;



console.log(outBlock); //1


console.log(inBlockWithVar); // not defined, as it should be

thankyou everyone

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).