What is the scope of Javascript Variables

April 19, 2020 By Nobody No Body

The Javascript variables are classified into 2 types. They are local variables and global variables. When we call a function in Javascript, we can assign some variables inside the function. Such variables are considered as local variables because the scope for using them is limited inside the function. These variables cannot be called outside the function because they are limited to the function itself. Hence naming them local variable.

Global variables are defined outside the function and they can be called in entire script. Irrespective of the function they are not limited to certain script, Hence naming the Global variables. There is an option to name the same var with different values inside function and outside function. The variable which is named inside function will be limited to the function and considered as local variable where as the variable which is assigned outside function can be accessed from anywhere because it is a global variable. Though this can be done, developers suggest not to use the same name for assigning variables because it will confuse the programmers.

Java script in browsers usually run in Strict mode. In strict mode the variables which are written outside function and not assigned value are not considered as global variables. Variables must certain value to be considered as global variables.