JavaScript Tutorials:Variables,If-else,Loops

In the basic of JavaScript tutorials ,we will learn basics such as variables,if and if-else,loops etc.

Let’s start ,

JavaScript Variables

The work of variables is same in JavaScript like other languages C,java etc.

Variables are the container in which data is kept.

It is the memory location of the name.

Before declaring the variables in the JavaScript,we must need to declare var keyword.

Example:

<script type="text/javascript">
var name;
var id;
</script>

In the one var keyword we can create multiple variables:

<script>
var name,id;
</script>

Some rules to declare the JavaScript variables:

  • Variables names must be start with letter(a to z or A to Z),underscore( _ ),or dollar-sign($).
  • case-sensitive means a &A is different.
  • First letter cannot be digits(0 to 9).

Declaration of JavaScript variables:

  • var x=20; //This is true
  • var _name=”suman”;//This is true
  • var 1mage=”suman”;//This is false
  • var image**=123;//This is false

Example of JavaScript:

Multiply the two numbers:

<script>
var p=10;
var q=20;
var r=p*q;
document.write(r);
</script>

Output:

200

Types of Variables in JavaScript

There are two types of variables in JavaScript:

  • Local Variable
  • Global Variable

Local Variable:

When the variable is declared inside the function then it is known as local variable.

It’s scope is valid only within the function or block.

<script>
function hello()
{
var x=20;//This local variable
}
</script>

Global Variable:

When the variable is declared outside the function then it is known as global variable.

It’s scope is valid outside the function or block.

<script>
var x=20;//This is global variable
function hello()
{
var y=5;
}
</script>

JavaScript If-else Statement

If-else statement is used to execute code/program according to the condition true or false.

We can set the condition that if condition is true then show “hello JavaScript” otherwise “Sorry JavaScript”.

<script>
var x=10;
var y=10
if(x==y)
{
document.write("hello JavaScript");
}
else
{
document.write("Sorry JavaScript");
}
</script>

Output:

hello JavaScript

Types of if-else statement

There are the three types of if-else statement which is given below:

  • If statement
  • if-else statement
  • if…else-if statement

If statement

It evaluates the expression/condition when the condition is true.

Example:

<script>
var x=10;
var y=20;
if(y>x)
{
document.write("This condition is true");
}
</script>

Output:

This condition is true

If-else statement

It evaluates the condition/expression in both case whether condition/expression true or false.

Example:

<script>
var x=10;
var y=20;
if(x>y)//This condition is false
{
document.write("Hello");
}
else
{
document.write("Hii");
}
</script>

Output:

Hii

If…else-if statement

It evaluates the expression when the condition is true from the multiples statement/expression.

Example:

<script>
var y=30;
if(y==10)
{
document.write("Hello");
}
else if(y==50)
{
document.write("Hii");
}
else if(y==30)
{
document.write("This is equal to 30");
}
else
{
document.write("No condition meet");
}
</script>

Output:

This is equal to 30

JavaScript Loops

JavaScript loops are same as other programming languages.

JavaScript loops are used to execute the blocks of codes /statement.

JavaScript loops run continuously when condition meet then the loop terminate.

Types of loops

There are four types of loops in JavaScript:

  • while loop
  • do-while loop
  • for loop
  • for- in loop

1: while loop

It will run infinite times if condition not meet whether condition meets then loop will be terminate.

syntax:

while(condition)
{
//Code will be execute here
}

Example:

<script>
var x=5;
while(x<=10>
{
document.write(x+"</br>");//Here </br> is used for nextline or newline
x++;
}

Output:

5
6
7
8
9
10

2: do-while loop

It will execute at least once whether condition true or false.

syntax:

do
{
//code will execute here
} while(condition);

Example:

<script>
var x=5;
do
{
document.write(x+"</br>");
x++;
}while(x<=10);
</script>

output:

5
6
7
8
9
10

3:for-loop

It provides the facilitate to initialize,condition check and increment/decrement in same line.

syntax:

for(initialize;condition;increment/decrement)
{
//code will execute here
}

Example:

<script>
var x;
for(x=1;x<5;x++)
{
document.write(x+"</br>");
}
<script>

output:

1
2
3
4

4: for-in loop

This loop is used to execute the properties of objects.

syntax:

for(var in object)
{
//code will execute here
}

Example:

<script>
var language={p:1,q:2};
var k=" ";
var s;
for(s in language)
{
k+=language[s];
}
document.write(k);
</script>

Output:

"12"

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

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