JavaScript Variable

variable in javascript

JavaScript Variable are containers for storing data values. The value of a variable can change throughout the program. there are two types of variable:Local variable & global variable.

Use the var keyword to declare a variable:


Example:

<script>

var x = 10;

var y = 20;

var z=x+y;

document.write(z);

</script>

JavaScript local variable

A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.
For example:

<script>

function abc(){

var x=10;//local variable

}

</script>


Or,

<script>

If(10<13){

var y=20;//JavaScript local variable

}

</script>

JavaScript global variable

A Global Variable has global scope which means it can be defined anywhere in your java script code.
For example:


<script>

var data=200;//gloabal variable

function a(){

document.writeln(data);

}

function b(){

document.writeln(data);

}

a();//calling JavaScript function

b();

</script>

JavaScript Reserved Words

They cannot be used as JavaScript variables, functions, methods, loop labels, or any object names.

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *