Important Notice:

Types of Variable

Types of Variable

30 views 2 min read

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 के नाम रखने के नियम:

  • आपके variables का नाम किसी letter, underscore या $ sign से शुरू होना चाहिए।
  • किसी भी variables का नाम number से start नहीं करना चाहिए, ये allowed नहीं है।
  • पहले अक्षर के बाद हम अंक (0 से 9) का उपयोग कर सकते हैं, उदाहरण: value1, v1
  • Variables में spaces नहीं हो सकते।
  • Variables के name reserved keywords नहीं होने चाहिए।
  • JavaScript में Variables Case Sensitive होते हैं। उदाहरण: name, NAME, Name तीन अलग variables हैं।

नोट: वेरिएबल बनाते समय उसे कोई शुरुआती मान (initial value) भी दे सकते हैं। इसे initialization कहते हैं।

Syntax:     var <name> = <value>;

var x = 100;
x + 102;   // परिणाम 202

Rules for naming variables:

  • Your variable name must start with a letter, an underscore (_), or a dollar sign ($).
  • A variable name must not start with a number – this is not allowed.
  • After the first character, you can use digits (0 to 9). Example: value1, v1.
  • Spaces are not allowed in variable names.
  • Variable names must not be reserved keywords.
  • JavaScript variables are case‑sensitive. For example, name, NAME, and Name are treated as three different 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

Related Notes