Wednesday, 27 April 2016

JavaScript Objects

A javaScript object is an entity having state and behavior (properties and method).
JavaScript is an object-based language. Everything is an object in JavaScript.
JavaScript is template based not class based. Here, we don't create class to get the object. But, we direct create objects.

Creating Objects in JavaScript
There are 3 ways to create objects.

  1. By object literal
  2. By creating instance of Object directly (using new keyword)
  3. By using an object constructor (using new keyword)
1) JavaScript Object by object literal

Syntax:
object={property1:value1,property2:value2.....propertyN:valueN}  

2)By creating instance of object 
Syntax:
var objectname=new Object();  

3) By using an Object constructor
The this keyword refers to the current object.

Example:
<script> 
function student(id,name,age){
this.id=id;
this.name=name;
this.age=age;
}
st=new student(103,"Vimal Jaiswal",12);
 
document.write(st.id+" "+st.name+" "+st.age);
</script> 


Javascript Functions


JavaScript functions are used to perform operations. We can call JavaScript function many times to reuse the code.

Syntax:
function functionName([arg1, arg2, ...argN]){
 //code to be executed
}  

Example:
function firstFunction(x1, x2) {
    return x1 * x2;              // The function returns the product of x1 and x2
}


Function Arguments

We can call function by passing arguments. Let’s see the example of function that has one argument.


Example:
function getVal(num){
alert(num*num*num);
}  

Function with return value

Example:
<script> 
function getData(){
return "hello first program";
}
</script> 
<script> 
document.write(getData());
</script>

Monday, 18 April 2016

Javascript Loops


The JavaScript loops are used to iterate the piece of code using for, while, do while or for-in loops. It makes the code compact. It is mostly used in array.

That means,
Instead of writing,
val+= name[0] + "
"
;
val+= name[1] + "
"
;
val+= name[2] + "
"
;

you can use
for (i = 0; i < name.length; i++) {
    val+= name[i] + "
"
;
}

Different Kinds of Loops
JavaScript supports different kinds of loops:

  • for - loops through a block of code a number of times
  • for/in - loops through the properties of an object
  • while - loops through a block of code while a specified condition is true
  • do/while - also loops through a block of code while a specified condition is true

1) JavaScript For loop: 

The JavaScript for loop iterates the elements for the fixed number of times. It should be used if number of iteration is known.

syntax:
for (initialization; condition; increment)
{
    code to be executed
}  

example:
for (i = 0; i < 5; i++) {
    val+= "The value is " + i + "
"
;
}

2)The For/In Loop

The JavaScript for/in statement loops through the properties of an object:

example:
var name= {fname:"Ravi", lname:"k"umar, age:25};
var val= "";
var x;
for (x in person) {
    val+= name[x];
}

3) JavaScript while loop

The JavaScript while loop iterates the elements for the infinite number of times. It should be used if number of iteration is not known. 
syntax:
while (condition)
{
    code to be executed
}  
example:
<script>
var i=10;
while (i<=15)
{
document.write(i + "<br/>");
i++;
}
</script>

4) JavaScript do while loop

The JavaScript do while loop iterates the elements for the infinite number of times like while loop. But, code is executed at least once whether condition is true or false. 
syntax:
do{
    code to be executed
}while (condition);  

example:
<script>
var i=20;
do{
document.write(i + "<br/>");
i++;
}while (i<=25);
</script>

Saturday, 9 April 2016

Use of break statement with switch

How break is useful with switch statement in Java?


               The break statement is used to terminate the statement sequence in a switch statement. If we do not use break statement, the control of flow of the switch continue as usual and just work like an else-if ladder. 

               The below program print the month name on the screen according to the provided number. 

Program:


import java.util.Scanner;
public class SwitchBreakDemo {
    public static void main(String[] args) {

        int month;
        System.out.println("Enter a number to display its corresponding Month Name");
        Scanner in = new Scanner(System.in);  
        month=in.nextInt();
        String monthString;
        switch (month) {
            case 1:  monthString = "January";
                     break;
            case 2:  monthString = "February";
                     break;
            case 3:  monthString = "March";
                     break;
            case 4:  monthString = "April";
                     break;
            case 5:  monthString = "May";
                     break;
            case 6:  monthString = "June";
                     break;
            case 7:  monthString = "July";
                     break;
            case 8:  monthString = "August";
                     break;
            case 9:  monthString = "September";
                     break;
            case 10: monthString = "October";
                     break;
            case 11: monthString = "November";
                     break;
            case 12: monthString = "December";
                     break;
            default: monthString = "Invalid month";
                     break;
        }
        System.out.println(monthString);
    }
}

Output:


Enter a number to display its corresponding Month Name
5
May