Friday, October 6, 2017

Global Local variable and Math operators



Global and Local variable:

Global variable:
Any variable that is created outside the Function.
The global variable can be called inside or outside the function.
<script type=”text/javascript”>
var game = “Table Tennis”;
function firstPlay()
{
document.write (game);
}
firstPlay();
document.write(game);

Result:
Table Tennis Table Tennis

 
Local variable:

Any variable that is created inside the Function
The local variable can be called only inside the function.
<script type=”text/javascript”>
function firstPlay()
{
var game = “Table Tennis”;
document.write (game);
}
firstPlay();
document.write(game);

Result:
Table Tennis


Math Operators:

Below are the math operators in JavaScript:
Addition +
Subtraction -
Multiplication *
Division /
Modulus %
Increment ++
Decrement --


<script type=”text/javascript”>
var add = 25 + 4 ;
document.write(add);
</script>

Result: 29

<script type=”text/javascript”>
var subtract = 29 - 4 ;
document.write(subtract);
</script>

Result: 25

<script type=”text/javascript”>
var multiply = 25 * 4 ;
document.write(multiply);
</script>

Result: 100

<script type=”text/javascript”>
var divide = 12 / 4 ;
document.write(multiply);
</script>

Result: 3

<script type=”text/javascript”>
var divide = 15 / 4 ;
document.write(multiply);
</script>

Result: 3.75

<script type=”text/javascript”>
var modulus = 25 % 4 ;
document.write(modulus);
</script>

Result: 1


<script type=”text/javascript”>
var apple = 10 ;
apple++;
document.write(apple);
</script>

Result: 11

<script type=”text/javascript”>
var apple = 16 ;
apple--;
document.write(apple);
</script>

Result: 15

Functions

Calling a Function in new Function

In JavaScript, a user can call a Function from another Function such as:
<script type=”text/javascript”>

function firstCandy()
{
document.write (“ Hersheys”);
}
function secondCandy()
{
document.write (“ Toblerone”);
}
function goodCandy(){
firstCandy();
secondCandy();
}
goodCandy();
</script>

Output:
Hersheys Toblerone

Calling a Function within Function:

When we try calling the Functions within each other, it prints results like never ending cycle. It is bad programming.

<script type=”text/javascript”>

function firstCandy()
{
document.write (“ Hersheys”);
secondCandy();
}
function secondCandy()
{
document.write (“ Toblerone”);
firstCandy();
}

firstCandy();

</script>

Output:


Hersheys Toblerone Hersheys Toblerone Hersheys Toblerone Hersheys Toblerone
Hersheys Toblerone...