Variable in JavaScript
In programming, we often need a named storage location to store the data or values. Using
variables, we can store the data in our program and access it afterward. In this article, we will learn
about variables in programming, their types, declarations, initialization, naming conventions, etc.
Variables in Programming
What are Variables In Programming?
A Variables In Programming is a named storage location that holds a value or data. These values
can change during the execution of a program, hence the term “variable.” Variables are essential for
storing and manipulating data in computer programs. A variable is the basic building block of a
program that can be used in expressions as a substitute in place of the value it stores.
Declaration of Variables In Programming:
In programming, the declaration of variables involves specifying the type and name of a variable
before it is used in the program. The syntax can vary slightly between programming languages, but
the fundamental concept remains consistent.
C++
Java
Python3
C#
Javascript
// Syntax: var/let/const variable_name;
var age;
let price;
const grade = 'A'; // Note: Constants need to be initialized during declaration.
Initialization of Variables In Programming:
Initialization of variables In Programming involves assigning an initial value to a declared variable.
The syntax for variable initialization varies across programming languages.
C++
Java
Python
C#
Javascript
// Initialization using var (older syntax)
var age = 25;
// Initialization using let (block-scoped)
let price = 10.99;
// Initialization using const (block-scoped constant)
const grade = 'A';
Types of Variables In Programming:
1. Global Variables:
Global variables in programming are declared outside any function or block in a program
and accessible throughout the entire codebase. In simpler words, Global variables can be accessed
in any part of the program, including functions, blocks, or modules.
C++
Java
Python3
C#
Javascript
// Define a class named GFG
class GFG {
// Define a static variable
static globalVariable = 5;
// Define a static method
static gfgFnc() {
console.log(GFG.globalVariable);
// Define the main method
static main() {
console.log(GFG.globalVariable);
// Call the static method gfgFnc
GFG.gfgFnc();
// Call the main method of the GFG class
GFG.main();
Output
2. Local Variables:
Local variables in programming are declared within a specific function, block, or scope and are
only accessible within that limited context. In simpler words, Local variables are confined to the
block or function where they are declared and cannot be directly accessed outside that scope.
C++
Java
Python3
C#
Javascript
function gfgFnc() {
var localVariable2 = 10;
console.log(localVariable2);
function main() {
var localVariable1 = 5;
console.log(localVariable1);
gfgFnc();
if (true) {
var localVariable3 = 15;
console.log(localVariable3);
// Call the main function
main();
Output
10
15
Difference between Variable and Constant:
Characteristic Variable Constant
A variable is a symbol that
A constant is a symbol that
represents a value that can
represents a fixed,
change during program
unchanging value.
Definition execution.
Can be changed or reassigned
Cannot be changed once
during the execution of the
assigned a value.
Mutability program.
Must be declared before use, Must be assigned a value at
and its value can be initialized the time of declaration, and
at declaration or later in the its value cannot be changed
Declaration and Initialization code. afterward.
Examples int count = 5; const double PI = 3.14159;
Used for storing values that Used for storing fixed values
may vary or change during or parameters that should not
Use Cases program execution. be modified.
Allocates memory space to
Allocates memory space to
store the value, similar to
store the value.
Memory Allocation variables.
dataType variableName = const dataType constantName
Syntax value; = value;
Difference between Local variables and Global Variables:
Characteristic Global Variables Local Variables
Confined to the block,
Accessible throughout the
function, or scope of
entire codebase
Scope1 declaration.
Characteristic Global Variables Local Variables
Accessible by any part of the
Accessible only within the
program, including functions,
limited context of declaration.
Visibility blocks, or modules
It exists only during the
Exist for the entire duration
execution of the block or
of the program
Lifetime function.
May have a default value, can May not have a default value,
be initialized outside and must be explicitly
Initialization functions or blocks initialized within the scope.
Accessible directly from any Directly accessible only within
Access from Functions function or block the declaring function