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.
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
let outBlock = 1;
function fun1() {
inBlockNoVar = 2;
let inBlockWithVar = 3;
}
fun1();
console.log(outBlock); //1
console.log(inBlockNoVar);//2
console.log(inBlockWithVar); // not defined, as it should be
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.