JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – var in a block scope work

JavaScript Quiz – var in a block scope work

Block Scope –

Block scope is a javascript scope which is lies within the curly brackets like if, switch conditions, and for or while loops.

So, it’s like whenever you see ({}) curly brackets. It’s a block scope. There is some variable declaration keyword which supports block scope and some not.

Block scope means variable declare within block scope can only be used within that scope not outside of it.

It’s syntax look like –

{ …Block Scope }

For more detail about JavaScript Block Scope click here.

JavaScript Question

In this javascript quiz code, you need to find out does var in a block scope work as we declare same variable in global and block scope.

First, we declare a variable string using the var keyword and assign a javascript string to it. Which is “JavaScript

var string = “JavaScript”;

Now, we create a block scope using curly brackets and create another variable inside of it.

{ …Block Scope }

In the next step, we create another variable string with the same as the previous one but inside a block scope. And assign a string value to it but different from the previous one.

{ var string = “JS Startup”; }

Finally, you need to find out the output of the console log in which string variable is pass. But as we have 2 string variable with same name but different values.

And also, one declares in the global scope and another one declares in block scope. So any idea what should be the output of the console log.

Answer – “JS Startup

Click To Run Code


Now, you know the answer is “JS Startup“. But how it’s working. Let me explain.

So, block scope is introduce in ES6 as new variable declaration let, const used that scope. Before block scope only global and function scope is there.

As, var only support global and functional scope but not support block scope.

And you can also declare the same variable name with var if ‘use strict‘ not used.

So, when we again declare a variable string and assign “JS Startup” to it in a block scope. It actually change the value of global scope also. As var not support block scope.

At last, That’s why we get an answer “JS Startup“.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JS Quiz – new set object can return the same size
  2. JavaScript Quiz – can break statement work on array forEach method
  3. JS Quiz – variable can be key and value in object
  4. JavaScript Quiz – array filter method can filter odd value


To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
Share via
Copy link