What is the scope of javascript variables ?

Javascript variables do have two scopes only.

  1. Local variables
  2. Global variables

What are Local variables in Javascript?

Local variables are the variables that are introduced within a function or a block of code or a scope which is undefined for the rest of the code. It means the only specific block of code knows that variable but is unknown for rest of the code.

A daily life Example:

You live in your home and have multiple things placed into but all the items and stuff present inside is only known to you and your family would term in local variable in javascript.

Code:

function myFunction()

{

                x=0

                y=”I love pakistan”

console.log(x)

console.log(y)

}

myFunction() //calling the function

In above example the variable x and y are defined within the block of code called function. If I call them out side of the block it will throw an error that variable is or variables are undefined in javascript. This is because “x” and “y” are local variables and are undefinable when outside the code block.

What are Global variables in javascript?

Variables that are accessible at any point or line of code is called global variable. Global variables are declared outside the block of scope and is easily accessible at any point. Global variable is easily and usually visible throughout the program unless it is shadowed.

Real world example:

Let suppose Home A and home B are neighbors to each other. A and B has to go to shop always to buy something for their home or self use. For their ease, A and B decided to construct a small road which they can easily use to go to a shop. They decided and constructed the road instead of constructing it locally from their home or else. Now, A and B and as well as all those who belong to the premises or who will buy a home next or beside to A and B and  wish to use this road to go to shop can use it will use it thus that would be considered as a global variable.

Data types in javascript

Types of variables have nothing to do with either local variable or global variable. This is because local variable and global variable are the scope of javascript variables. Types of variables are mainly considered as data type. There are mainly six f data types in javascript which are categorized in to three main groups or component.

  • Primitive Datatype
    • INT
    • String
    • Float
  • Composite Datatype
    • Object
    • Array
    • Function
  • Special Datatype
    • Undefined
    • NULL

We will discuss the data type in depth in another tutorial. Just keep in mind that the primitive datatype or primary datatype are mainly used in throughout javascript basic programming. However, we will cover all the composite datatype including Objects in javascript, Arrays in javascript and functions in javascript.

Buy on amazon with upto 40% off

RECOMMENDED FOR YOU

What is javascript?

1 thought on “Variables in Javascript

Leave a Reply

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