Home JavaScript JavaScript Variables

JavaScript Variables

by anupmaurya
5 minutes read

In this article, you’ll learn about JavaScript Variables, here we will mostly talk about local ,global and Scope of Variable.

What is JavaScript Variable?

A JavaScript variable is nothing more than a name for a storage area. Imagine a computer memory as your kitchen, where you organise each item in a specific order so that you may quickly access it when you need it.

So the same thing happens with a computer it has n number of storage when you want to put some data in it you need some placeholder or container that can do it for you. so variable is that container.

JavaScript includes variables that hold the data value and it can be changed on runtime as JavaScript is dynamic language.

You can use var, const, and let keyword to declare a variable, and JavaScript will automatically determine the type of this variable according to the value passed.

How to Declare Variables in JavaScript

In JavaScript, we can declare variables in three different ways like this

// Using var
var price = 100;

// Using let
let isPermanent = true; 

// Using const
const JsPublication= 'HappyLearning' ;

Rules for declaring a JavaScript variable

JavaScript variable (also known as identifiers).

  • Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
  • After first letter we can use digits (0 to 9), for example value1.
  • JavaScript variables are case sensitive, for example x and X are treated as different variables.

Let’s understand with example,

Correct JavaScript variablesIncorrect JavaScript variables
var x = 20.0;
var _language=”JavaScript”;
var 123=30;
var *aa=320;

Types of variables in JavaScript

JavaScript Variables
Types of Scope in JavaScript

1. Local Variable

  • Variables that initialised only inside a block or function are called Local variables.
  • They have no presence outside the function.
  • The values of such Local variables cannot be changed by the main code or other functions. This results in easy code maintenance and is especially helpful if many programmers are working together on the same project.

2. Global Variable

  • Variables that exist throughout the script are called Global variables.
  • These variable’s value can be changed anytime in the code and even by other functions.

What is Variable Scope?

Local variables exist only inside a particular function hence they have Local Scope. Global variables on the other hand are present throughout the script and their values can be accessed by any function. Thus, they have Global Scope.

Types of Scope in JavaScript

Types of Scope in JavaScript
Types of Scope in JavaScript

1. Local Scope Variables

  • Any variable that you declare inside a function is said to have Local Scope.
  • You can access a local variable within a function. If you try to access any variable defined inside a function from outside or another function, it throws a ReferenceError.
function leaders()
{
 let name="Anup Maurya";        //Local Variable
console.log(name);       
}
leader();

console.log(name);                     //ReferenceError: name is not defined.

2. Globle Scope Variables

  • Any variable declared outside of a function is said to have Global Scope.
  • In simple terms, a variable that can be accessed anywhere in the program is known as a variable with global scope. Globally scoped variables can be defined using any of the three keywords: let, const, and var.
var name="Anup Maurya";          //Global Variable
console.log(name); 

function leader(){
    console.log(name);                //Accessing a global variable.
}
leader();

Having fun learning JavaScript!

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.