Variables in JavaScript

JS - Variables
Variable เคเค• Box เค•े เคœैเคธा เคนोเคคा เคนैं, เคœिเคธเคฎें เค•ोเคˆ Value เคฏा Information เค•ो Store เค•เคฐ เค•े เคฐเค–ा เคœा เคธเค•เคคा เคนैं JavaScript เคฎें Variable Create เค•เคฐเคจे เค•े เคฒिเค เคนเคฎें เค•ोเคˆ date type define เค•เคฐเคจे เค•ी เค†เคตเคถ्เค•เคคा เคจเคนीं เคนोเคคी เคนैं,Variable เคชเคฐ เค•िเคธी เคญी เคช्เคฐเค•ाเคฐ เค•ा date type Value เคฏा Information Store เค•िเคฏा เคœा เคธเค•เคคा เคนैं|

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 เค•िเคฏा เค—เคฏा เคนैं|

  1. Local Variable
  2. 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

close