Javascript Variable

tutorial and example
tutorial and example

Variables are used to store values (name=”Ram”) or expressions (Sum=x+y).

Before using of variable first we need to declare it. We use keyword var to declare a variable like this:

There are two types of variables:

  1. Local Variable
  2. Global Variable

Local Variable– It is declared inside block or function.

Example

Global Variable-It has global scope which means, it can be defined anywhere in JavaScript code.

A variable is declared outside the function.

Example

Try Now

Declaring JavaScript global variable within function

To declare JavaScript global variable inside function, we need to use window object.

Example

Now it can be declared inside any function and can be accessed from any function.

Example

Try Now