JavaScript – Variables

What is a Variable ?

Variable is the one which is liable to change, it prefers changing from one value to another.

Variables in programming language have the same qualities as mentioned above.

What is a variable in a programming language ?

Variable is simply a value which has the ability to change itself depending on the conditions passed to the program.

Variables store information to be referenced and manipulated inside the computer program.

Variables are also called containers that hold information

What is a JavaScript variable ?

JavaScript variables hold the data value and it can be modified/changed at anytime.

JavaScript uses reserved keyword “var” for declaring a variable.

We can assign a value to a variable by using the equal to (=) operator.

Example :

var a = 10;
var b = 20;
var name = "Abdul Kalam";

How JavaScript variables are stored ?

Usually JavaScript variables are stored in two places.

1. Stack

Stack is a continuous region of memory allocation, allocated for storing variable values for each and every execution of a JavaScript function.
Stack is considered as the static allocated memory.

2. Heap

Heap is a bigger region that stores data which is allocated dynamically.
Heap is considered as the dynamically allocated memory.

Javascript advanced course !!!

Click to Learn More about – Javascript online learning

What are the rules to be followed for declaring a variable ?

There are certain rules to be followed when declaring a JavaScript variable.

Some of them are listed below.

1. Variable names cannot contain spaces.

2. Variable names must begin with a letter, an underscore (_) or a dollar sign ($).

3. Variable names can only contain letters, numbers, underscores, or dollar signs.

4. Variable names are case-sensitive.

5. There’s no limit to the length of the variable name.

6. There are certain reserved key words which may not be used as variable names.

Because these reserved key words have other meanings within JavaScript.

The complete list of the reserved words is listed below in this tutorial.

What is a scope of a variable ?

Scope is defined as the accessibility/visibility of variables.

Each JavaScript function creates a new scope to the variables.

What are the types of scope in JavaScript ?

There are two types of scopes in JavaScript

1. Local Scope
2. Global Scope

What is a local scope variable in JavaScript ?

Local scope variables is only visible within a function where it is defined.

Local variable function parameters are always local to that function.

Local variable can effectively take precedence over a global variable with the same name within the body of the function.

Suppose if we declare a local variable with the same name of the global variable, This local variable can hide the global variable.

Example :

<!DOCTYPE html>
	<title>Our Title</title>
		function callMe(){
   		var a = 10; //local variable

   		function callFunc(){  console.log(a); }
     		callFunc(); //executes local function

Output :

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