DEV Community

Cover image for Scoping in JavaScript
Srishti Prasad
Srishti Prasad

Posted on • Edited on

2

Scoping in JavaScript

What is scope?

Scope is all about code accessibility. It determines which parts of the code are accessible and which parts are inaccessible. In this blog you'll learn about how the scope chain works within JavaScript. You will also explore some of the different scope types, such as global and local.
Global Scope: The code that exists outside of a function is referred to as global scope, and all
Local Scope: The code inside of a function is known as local scope or function scope. If a variable is defined within a function, then you can say it's scoped to that function. This is also known as local scope.

In the ES5 version of JavaScript, only functions can build local scope. The only way to declare a variable in JavaScript was to use the var keyword. First, you can use it in your code even before it is declared. Also, you can redeclare the same variable when you use VAR.

The ES6 version of JavaScript introduced a new variety of scope known as the block scope. Block scope states that a variable declared in a block of code is only accessible inside that block. All the other code outside of the code block cannot access it. Block scope is built when you declare variables using let or const. The scope of these variables is contained within curly braces. Its syntax is very similar to the var syntax. Only the keyword is replaced.

Let's learn about the var, let, and const variables and the different rules that they are bound to.

1)var

A variable declared with the var keyword can be accessed before initialization as long as the variable is eventually initialized somewhere in our code.
let's try to console log a variable that hasn't been declared or initialized.
Image description
Error: Reference Error

If we declare the var variable and don't initialize it then it won't give any error rather it prints "undefined"

Image description

Also, var variable can be redeclared

var user ="Neha"
var user = "Sunita"
var user = "Aditi"

console.log(user);

var user
//user will be updated and will take var value
Enter fullscreen mode Exit fullscreen mode

Image description

2) let

we CANNOT access a let variable before we declare it

console.log(user);
let user 
Enter fullscreen mode Exit fullscreen mode

Image description

We CAN'T redeclare a let variable

let user;
let user="Sam" // ERROR 

```


But we CAN re-assign it



```javascript
let user="Jerry"
user = "Jane";
console.log(user);
```



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bfipxcdqopi7ktl1s3gd.png)

##3)const 
we CAN'T access the const variable BEFORE initialization


```javascript
const user;
console.log(user);// ERROR

```



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/daujas5pujijal89uuxq.png)
Although, we CAN declare an unassigned variable with let


```javascript
let user;
console.log(user);
```



we CAN'T redeclare a const variable



```javascript
const user="Andrew";
console.log(user);

user = "John"
console.log(user);
```



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fiijsnqr61lb8m3ji2i2.png)

If you have any query let me know in the comment section. I'll try my best to answer them.
If you find this blog helpful, please ❤️ like it.
You can follow me if you wish to enhance your fundamental knowledge of JavaScript
In the coming blogs I'll be covering most asked concepts of JavaScript.
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay