Javascript | Variable & Variable Types By. Shivansh Sir

 JAVASCRIPT

JAVASCRIPT | VARIABLE

Javascript Variables में information को एक जगह पर store किया जाता है | Variable पर कौनसे भी data types की value store की जाती है |

RULES FOR CREATING VARIABLE

  1. Variables को declare करने के लिए 'var' keyword का इस्तेमाल किया जाता है |
  2. Variables के नाम की शुरुआत किसी भी letter(A-Z a-z) से, underscore(_) से या dollar($) sign से की जाती है |
  3. Variables की शुरुआत Numeric से नहीं हो सकती | लेकिन Variable alphanumeric हो सकता है | For eg. A1 = 5
  4. Variables Javascript का कोई keyword नहीं हो सकता |
  5. Variables case-sensitive होते है | 'A' और 'a' ये दोनों अलग-अलग variables है |

Example for Variable JS

Source Code

<script type="text/javascript">

var a = 5;

document.write("Value of a is " + a);

</script>

Output

Value of a is 5

JAVASCRIPT | VARIABLE DECLARATION IN JS

Variable जब declare किया जाता है, तब undefined value मिल जाती है | मतलब variable पर कोई value store नहीं की गयी है |

Source Code

<script type="text/javascript">

var a;

document.write("Value of a is " + a);

</script>

Output

Value of a is undefined

JAVASCRIPT | VARIABLE INITIALIZATION

जब variable create किया जाता है तब भी variable intialised किया जाता है और variable declared करने के बाद भी initialized किया जाता है |

Source Code

<script type="text/javascript">

var a;

a = 5;

var b = 10;

document.write("Value of a is " + a + " ");

document.write("Value of b is " + b + " ");

</script>

Output

Value of a is 5

Value of b is 10

#एक ही 'var' keyword के साथ multiple variables ,(comma) से declare या initialize किये जा सकते है |

Source Code

<script type="text/javascript">

var a = 5, b, c = 10;

</script>

JAVASCRIPT | OVERWRITE VARIABLE VALUE

Javascript में variable की value को overwrite किया जाता है |

Source Code

<script type="text/javascript">

var a = 5;

document.write("Value of a is " + a + "

");

var a = 10;

document.write("Value of a is " + a);

</script>

Output

Value of a is 5

Value of a is 10

JAVASCRIPT | TYPES OF VARIABLE

Variables के दो प्रकार होते है |

Local Variables

Global Variables

1. Local Variables

Local Variables सिर्फ Function के अन्दर ही visible होते है |

Source Code

<script type="text/javascript">

function func(){

var a = 5;

document.write("Value of a is " + a);

}

func();

</script>

Output

Value of a is 5

2. Global Variables

Global Variables Function के बाहर कहा भी access किये जाते है |

Source Code

<script type="text/javascript">

var a = 5;

function func(){

var a = 10;

}

document.write("Value of a is " + a);

</script>

Output

Value of a is 5


For Any Doubt Clear on Telegram Discussion Group

Join Us On Social Media
For Any Query WhatsApp Now

Comments