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