21 Javascript const block level scoping – JavaScript for Beginners

Last updated on July 13th, 2020 at 02:26 pm.

21 Javascript const block level scoping - JavaScript Tutorial 2018

This video is taken from the full course that will teach you HTML, CSS, Programming concepts and Javascript .

Video Transcript:

Hi, and welcome back.

So in the last video, we started looking at constants.

And the last thing I said was that ‘const’ is a block scope variable.

So, in this video I want to introduce some blocks, some kind of a block so that we can see exactly what the block scope behavior of ‘const’ is in JavaScript.

In the last videos, we haven’t really talked much about the block scoping.

Especially with the ‘let’ variable keyword.

But after we look at this video, you’re going to understand exactly what I mean.

So I want to introduce some kind of a block here and I don’t want to introduce a function.

So I’ll just do ‘if’.

I’ll just do an ‘if’ block.

So, let’s say ‘true’.

‘if(true)’, do the following statement.

So I will take this and put it within this block.

And this is going to help us.

This is going to help us explain a lot of things.

The reason I don’t want to use Scratchpad without any kind of block is because of some issues we experienced with you in the last videos whereby once I declare a variable and I run it, the variable is still stored in the browser memory.

So if I introduce a block, it will help us curb such kind of issues.

So here we have an ‘if’ block.

So basically, we’re going to look at these kinds of statements in future.

Don’t worry much about this.

I’m just using this so that I can explain the ‘const’.

But we are going to look at things like, ‘if’, ‘if-else’ in upcoming videos.

If not in upcoming videos, in upcoming courses.

So just bear with me.

So basically, what this does is ‘if’ true, do whatever is inside this block.

So, the only reason I’m using the ‘if’ is so that I can introduce a block.

So that’s a full block there.

And we know that ‘const’ as I said in the last video, ‘const’ is a block level variable.

And one thing I didn’t say in the last video is, once you declare a variable, once you declare a constant, ‘const’ with an identifier, you cannot use the same identifier to declare another variable.

So for example, let me say I try to say ‘var VAT;’.

If I try to do this, this’ll give me an error.

So if I try and run this, you see it says ‘redeclaration of const VAT’.

And that is the same for ‘let’.

Even if I change the name of this to ‘VAT2’ and ‘VAT2’.

So, and I try to run this, it will still give us the same error, ‘redeclaration of const VAT2’.

So you cannot use the same identifier that you’re using for a constant to declare another variable.

And you can also not use the same identifier to redeclare a function.

So if I say ‘function VAT2’, just that and let me remove this.

And if I try to run, ok.

Let me pull these ones here.

And if I try to run this, it also ‘missing ; before statement’.

Why is that? Okay, okay, okay.

This is ‘function’ not ‘fucntion’ whatever that is.

So, if I run this, you see ‘redeclaration of const VAT’.

So that basically means you cannot use the same identifier for a function, a variable within the same scope, if you’ve already used the same identifier for a constant.

So I hope you’ve understood the identifier thing, where you cannot use the same identifier for the same constant and also for a variable.

So if I delete this and then let me remove this ‘VAT2’ thing.

Here we said, we did it, all of this.

So maybe you might be thinking , oh let me change the value of my ‘const VAT’.

Maybe ‘VAT’ has changed.

So you want to change it and just come here and redeclare.

So if I run this, you’ll see ‘redeclaration of const’ is not allowed.

But, one thing about ‘const’ that I’ve already told you is that, it is a block level thing.

So if I create another ‘if’ and redeclare ‘const VAT’, there won’t be any issues.

And we’re going to see an exact example of that in the next video.

So in this video, just go through everything I’ve spoken about, about using the same identifier for variables and functions.

So I’ll see you in the next video.

This section is from Learn Javascript from Scratch.

Learn Javascript from Scratch

Comment Here

Need WordPress help? Linux Server help? Talk to us.

  • We are your own WordPress customer service.
  • We set up Linux servers and install or migrate WordPress. Learn more here.
  • We support WooCommerce too.
  • Check out our WordPress customer support plans here or contact us below .

If you have any questions regarding WordPress support, Linux server support or any of our services, feel free to reach out or read more on our services page.

Join this free course:

How to host multiple WordPress
websites on a VPS

Close me!