Rules for Creating Variables
- Variable को declare करने के लिए 'var','let' और 'const' keyword का use किया जाता हैं|
- Variable के नाम की शुरुआत किसी भी Letter(A-Z a-z) से, underscore(_) से या dollar($) sign से किया जा सकता हैं|
- Variable की शुरुआत Numeric या Number से नहीं हो सकती हैं, परन्तु Variable alphanumeric हो सकती हैं| Exp : a1=5;
- Variable JavaScript का कोई keyword नहीं हो सकता हैं|
- Variable Case-Sensitive होते हैं, 'A'और 'a' ये दोनों अलग-अलग Variable हैं|
Syntax for Variable
<script type="text/javascript"> var a = 5; let b = 10; const c = 3.14159265; </script>
var , let , const keyword
JavaScript में var, let और const दोनों वेरीएबल (variable) को declare करने के लिए उपयोग किए जाने वाले keywords हैं। लेकिन, वे पूर्णतया एक दूसरे से अलग होते हैं।
var :-
- var keyword से declared की गई वेरीएबल function scope में होती है।
- यदि हमें कोई वेरीएबल var keyword से declare नहीं किया है, तो वो globally scope में होगी।
- var वेरीएबल को re-declare या re-assign किया जा सकता है।
Code for var Keyword
var name = 'John'; console.log(name); var name = 'Jane'; console.log(name);
let :-
- let keyword से declared की गई वेरीएबल block scope में होती है।
- let वेरीएबल को re-assign किया जा सकता है, लेकिन re-declare नहीं किया जा सकता।
Code for let Keyword
let name = 'John'; console.log(name); let name = 'Jane'; // This will throw an error
const :-
- const keyword से declared की गई वेरीएबल block scope में होती है।
- const वेरीएबल को re-assign या re-declare नहीं किया जा सकता।
Code for let Keyword
const name = 'John'; console.log(name); const name = 'Jane'; // This will
Q :- Write a JavaScript Program for Variable.
Source Code :
<!DOCTYPE html.> <html> <head> <title>Variable</title> </head> <body> <script type="text/javascript"> var a=5; //Variable let b=10; //Variable const c=15; //Variable document.write("Value of a is "+a+"<br>"); document.write("Value of a is "+b+"<br>"); document.write("Value of a is "+c+"<br>"); </script> </body> </html>
Output :-
Value of a is 5 Value of b is 10 Value of c is 15
Variable Declration in JavaScript
Variable जब declare किया जाता हैं,और Variable पर कोई Value Store नहीं की जाती हैं, तब undefined value मिलती हैं|
Q :- Write a JavaScript Program for Variable Declration.
Source Code :
<!DOCTYPE html.> <html> <head> <title>Variable Declration</title> </head> <body> <script type="text/javascript"> var a; //Variable Declration document.write("Value of a is "+a); </script> </body> </html>
Output :-
Value of a is undefined
Variable Initialization in JavaScript
जब Variable Create किया जाता है तभी Variable Intialized किया जाता हैं, और Variable Declared करने के बाद भी Intialized किया जा सकता हैं|
Q :- Write a JavaScript Program for Variable Initialization.
Source Code :
<!DOCTYPE html.> <html> <head> <title>Variable Initialization</title> </head> <body> <script type="text/javascript"> var a; //Variable Declration a=5; // Variable Intialized document.write("Value of a is "+a); </script> </body> </html>
Output :-
Value of a is 5
'var' keyword
एक 'var' keyword के साथ multiple variables,(comma) से declare या initialize किया जा सकता हैं|
Q :- Multiple Variable in JavaScript.
Source Code :
<!DOCTYPE html.> <html> <head> <title>Multiple Variable</title> </head> <body> <script type="text/javascript"> var a=5,b,c=10; document.write("Value of a is "+a+"<br>"); document.write("Value of b is "+b+"<br>"); document.write("Value of c is "+c+"<br>"); </script> </body> </html>
Output :-
Value of a is 5 Value of b is undefined Value of c is 10
Overwrite Variable Value
Javascript में Variable की value को owerwrite किया जा सकता हैं|
Q :- Overwrite Variable in JavaScript.
Source Code :
<!DOCTYPE html.> <html> <head> <title>Overwrite Variable</title> </head> <body> <script type="text/javascript"> var a=5; document.write("Value of a is "+a+"<br>"); var a=10; document.write("Value of a is "+a+"<br>"); </script> </body> </html>
Output :-
Value of a is 5 Value of a is 10
Type of Variables in JavaScript
JavaScript में दो प्रकार के Variable होते हैं, इनको scope के according categories किया गया हैं|
- Local Variable
- Global Variable
Local Variable
इस Variable में scope किसी function तक limited रहता हैं, ऐसे variable किसी function के अंदर create किया जाता हैं, और वे उस function में ही काम करते हैं| function के बाहर हम variable को use नहीं कर सकते हैं, जिस block या function में ये variable create किये जाते है ये सिर्फ उसी में काम करता हैं ये उसका scope कहलाता हैं| यदि हम local variables को scope के बाहर access करने की कोशिश करते हैं तो undefine variable error generate होती हैं |
Q :- Local Variable in JavaScript.
Source Code :
<!DOCTYPE html.> <html> <head> <title>Local Variable</title> </head> <body> <script type="text/javascript"> function fun(){ var a=5; document.write("Value of a is "+a); } fun() </script> </body> </html>
Output :-
Value of a is 5
Global Variables
इस variable का scope पुरे program में होते हैं ऐसे variable program के शुरू में ही create किया जाता हैं और हम इसे पुरे program में कही भी use कर सकते हैं इस तरह के variable का scope पुरे program में होता हैं|
Q :- Global Variable in JavaScript.
Source Code :
<!DOCTYPE html.> <html> <head> <title>Global Variable</title> </head> <body> <script type="text/javascript"> var a=6; function fun(){ var a=11; } document.write("Value of a is "+a); </script> </body> </html>
Output :-
Value of a is 6
Post a Comment
If you have any doubts,Please let me know