Understanding Scope in Javascript

Hello Readers,

This article will help you to understand about the scope in javascript.

Below is the list of topic that is covered in this article:

· Introduction to Scope.

· What is the scope.

. Types of scope in javascript

So let’s see each and every topic one by one.

Introduction

Javascript has a feature called Scope. The concept of scope is not difficult if once understood. I will try my best to explain it.

What is Scope?

In simple words, scope determines the accessibility or visibility of variables, objects, and functions from different parts of code.

Types of Scope in Javascript

Javascript has basically two types of scope:

1) Local scope

2) Global scope

Local Scope: As the name suggests, the scope of the variable is limited to that particular function,means the variable cannot be accessed outside the function.

Local Scope

This gives an advantage as the same name can be used in different functions as the variables are bind with the function.

Global Scope: As the name suggests, the scope of a variable is not limited to one function, any function in the code can access the variable.

Global scope

A variable is in the global scope if defined outside the function.

Conclusion

The scope is a policy that manages the availability of variables. A variable defined inside scope is accessible only within that scope, but inaccessible outside.

These concepts are radical to JavaScript and important to understand if you want to approach more advanced topics. I hope you got a better understanding of JavaScript Scope and things around it. If something just didn’t click, feel free to ask in the comments below.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store