JavaScript Variables

JavaScript Variable is an object(or a named memory space) that is used to store a value that can be used during program execution. A variable in JavaScript, just like in other programming languages, has a name, a value, and a memory address.

  • The name of the variable uniquely identifies the variable,
  • The value refers to data stored in the variable, and
  • The memory address refers to the memory location of the variable.

In other words, we can say that variable is a container that can be used to store value and you need to declare a variable before using it.

In JavaScript, the var keyword is used to declare a variable. Also, starting from ES6 we can also declare variables using the let keyword.

JavaScript Rules for Variable name

The following are some rules to keep in mind while declaring variables.

  • Variable names cannot contain spaces.
  • The first letter of the variable can be [a-z, A-Z], dollar sign ($) or underscore(_), after the first letter of name any digit [0-9] can be used.
  • Variable names are case sensitive. For example: var a and var A both are different.
  • We can use either var or let keyword to define variables.
  • We can not use reserved words as the name of the variables in JavaScript.

JavaScript Syntax for Declaring a Variable

Following is the syntax for declaring a variable and assigning values to it.

var variable_name;
// or
let variable_name;

We can also define a variable without using the semicolon too. Also, when we have to define multiple variables, we can do it like this:

// declaring 3 variables together
var x, y, z;

And as we have already learned that Dynamic typed is a JavaScript feature, so we do not have to worry about specifying the data type of the value which we will store in the variable. JavaScript automatically detects that.

JavaScript Variable Example:

Now let’s take a simple example where we will declare a variable and then assign it a value.

var employeeName;   // Declaring a variable

var employeeName = "Rahul";   // Declaring and assigning at the same time

You can initialize or assign a value to the variable at the time you declare the variable or you can just declare the variable and initialize that later on.

JavaScript: Types of Variables

JavaScript supports two types of variables, they are:

  • Local Variable
  • Global Variable

You can use them according to the requirement in the application. Let’s learn about both JavaScript Local variables and JavaScript Global variables with examples.

1. JavaScript Local Variable

JavaScript Local variable is a variable that is declared inside a code block or a function body or inside a loop body and it has scope within the code block or the function. In simple words, the scope of a local variable is between the opening and closing curly braces { }, when declared and defined inside a code block or a function body.

Starting from ES6 it is recommended to use the let keyword while declaring local variables.

Let’s take an example of it.

function someFunc() { let num = 007; // local variable. document.writeln(num); } // calling the function someFunc(); /* trying to access the variable outside its scope */ document.writeln(“/”); document.writeln(num);

2. JavaScript Global Variable

JavaScript Global Variable is a variable that is declared anywhere inside the script and has scope for the complete script execution. Global variables are not declared inside any block or function but can be used in any function, or block of code.

var num = 007; // global variable. function someFunc() { document.writeln(num); } // calling the function someFunc(); /* trying to access the global variable */ document.writeln(“/”); document.writeln(num);

Its recommended that we use the var keyword to declare the global variables, starting from ES6.

2 thoughts on “JavaScript Variables

  1. Pingback: JavaScript Syntax

Leave a comment