Types of Variables in JavaScript
1. Local Variable
2. Global Variable
1. Local Variable (लोकल वेरिएबल):-
Definition (परिभाषा)
A variable declared inside a function or block is called a Local Variable.
जो variable किसी function या block के अंदर बनाया जाता है उसे Local Variable कहते हैं।
It can only be used inside that function/block.
इसे केवल उसी function या block के अंदर उपयोग किया जा सकता है।
example:-
function show() {
let a = 10; // Local Variable
console.log(a);
}
show();
2. Global Variable (ग्लोबल वेरिएबल):-
Definition (परिभाषा)
A variable declared outside all functions is called a Global Variable.
जो variable सभी functions के बाहर declare किया जाता है उसे Global Variable कहते हैं।
It can be used anywhere in the program.
इसे program में कहीं भी उपयोग किया जा सकता है।
Example:-
let a = 50; // Global Variable
function show() {
console.log(a);
}
show();
console.log(a);
Variables के नाम रखने के नियम:
नोट: वेरिएबल बनाते समय उसे कोई शुरुआती मान (initial value) भी दे सकते हैं। इसे initialization कहते हैं।
Syntax: var <name> = <value>;
var x = 100;
x + 102; // परिणाम 202
Rules for naming variables:
Note: When creating a variable, you can also assign an initial value to it. This process is called initialization.
Syntax: var <name> = <value>;
js
var x = 100; x + 102; // result 202