Showing posts with label online javascript course. Show all posts
Showing posts with label online javascript course. Show all posts

Friday, April 29, 2016

Control Statements in JavaScript

A conditional statement is a set of instructions that executes depending upon the condition is true or false.

As per MDN, JavaScript supports two conditional statements if…else and switch case.

JavaScript is a computer language mainly used for making Interactive websites under Web development. 
if statement:
Statements inside if will be executed if the expression is true. If the expression is false then no statements will be executed. We will be using comparison operators to evaluate the expression.
 
Syntax:
 
if (expression)
{
          statements will be executed only if the expression is true
}
Example:
<html>
<body>
      <script type="text/javascript">
         var age = prompt('Enter your age plz');
         if( age >=18 ){
         alert("Qualifies for driving");
         }
      </script>
</body>
</html>

Result:
Enter your age plz: 25
Qualifies for driving.
If we enter age greater than 18 then it will execute the statement otherwise will come out.

if ….else statement
It is the next form of control statements which allows to execute statements depending upon the expression is true or false.

Syntax:

if (expression)
{
           statements will be executed only if the expression is true
}
else {
          statements will be executed only if the expression is false
}
 
Example:
<html>
<body>
      <script type="text/javascript">
      var age = prompt('Enter ur age plz');
      if( age >=18 ){
      alert("Qualifies for driving");
      }
      else{
        alert(“Doesn’t qualify for driving”);
      }
       </script>
</body>
</html>
 
Result:
Enter your age plz: 25
Qualifies for driving
Enter your age plz: 15
Doesn’t qualify for driving

If age is greater than or equal to 18 then it will execute the if statement otherwise will execute the else statement.

if else if statement:

This is more advanced form of if..else that will execute statements on the basis of given conditions. It is useful in the case of multiple conditions. In this case only the first logical condition which is true will be executed.This can also be known as nested if statements.

Syntax:
if (expression1){
        statements will be executed only if the expression2 is true
}
else if (expression2){
       statements will be executed only if the expression2 is true
}
else if (expression3){
       statements will be executed only if the expression3 is true
}
else {
      statements will be executed only if the expression is false
}

Example:
<html>
<body>
      <script type="text/javascript">
      var book = prompt('Enter bookname');
      if( book = ‘Kiterunner’ ){
      alert("It comes under fiction category");
      }
      else if( book = ‘Wasted in Engineering’ ){
      alert("It comes under non fiction category");
      }
      else if( book = ‘A history of India’ ){
      alert("It comes under history category");
      }
      else{
     alert(‘Unknown category’);
      }
      </script>
</body>
</html>
 
Result:

It will display the result on the basis of the choices given. If we will enter Kiterunner in the prompt box, thenIt comes under fiction category’ will be the output. If we will enter any bookname which is not mentioned, example ‘Madhushala’, then result will be ‘unknown category’. For comparison use == (equal operator). For comparison don’t use =. ‘=’ is used for assigning values not for comparing them.

SWITCH CASE
The main objective of switch case is to execute different statements given on the basis of expression given. Each case statement will be checked against the expression given until a match is found. If nothing founds default statement will be executed. Break is very important after each statement. If we will not give it then every statement will be executed.
Syntax:
switch (expression)
{
    case condition 1: statement(s)
    break;
    case condition 2: statement(s)
    break;
    case condition n: statement(s)
    break;
    default: statement(s)
}
 
Example:

<!DOCTYPE html>
<html>
<head>
           <title>Switch Case</title>
</head>
<body>
<script type="text/javascript">
      var time = new Date();
      var myday = time.getDay();
      //alert(myday);
      switch(myday){
         case 0:
         alert('it is sunday');
         break;
         case 1:
         alert('it is monday')
         break;
         case 2:
         alert('it is tuesday')
         break;
         case 3:
         alert('it is wednesday')
         break;
        case 4:
        alert('it is thursday')
        break;
        case 5:
        alert('it is friday')
        break;
        case 6:
        alert('it is saturday')
        break;
       default:break;
       }
</script>
</body>
</html>

In the above example, new Date() function will display the current date and date with time zone. 
 
In the above example, new Date() function will display the current date and date with time zone. The getDay() function in JavaScript returns the weekday as a number between 0 and 6. (Sunday=0, Monday=1, Tuesday=2, Wednesday =3, Thursday=4, Friday=5, Saturday=6). Result will be current day and it will execute the case depending upon on which day it has been executed. For example. If we are executing it today, i.e. 21st April 2016. It will find the match with Case 4 and execute it. Result will be “it is Thursday”.

Monday, April 25, 2016

Understand Core Difference between JavaScript and jQuery

JavaScript is a computer language mainly used for making Interactive websites under Web development. It provides a rich user experience for the websites. It is used along with HTML and CSS. HTML defines the content and CSS is used to give appearance like background color, fonts etc to web page. The main purpose of JavaScript is to add behavior to the page without loading a new page. We only make a static website by using HTML and CSS, JavaScript add interaction with the visitor and improves visitor experience. JavaScript is also used not only over the web but also in PDF documents, desktop widgets etc.

jQuery is a JavaScript library having pre-written JavaScript codes. jQuery makes the coding easy for JavaScript web developers. It is the set of codes written in JavaScript. Every thing in jQuery is derived from JavaScript.

The core difference between them is that, JavaScript is a programming language and jQuery is library. jQuery is nothing without JavaScript. If we write code in JS it's takes time but in jQuery, there is no necessities to write much scripting.

JavaScript is based on Object Oriented Programming while jQuery is cross platform library to make client side scripting easy. jQuery allows to focus on the problem and no need to take care about the JS code. We can do complex thing by writing a single statement in the editor that if we do same in JS needs lots of coding and debugging.

Following are the main difference between JQ and JS:
  • JavaScript works with ECMA and DOM while jQuery has only DOM
  • Animation are possible with JQ
  • jQuery supports Firefox, Google Chrome and Internet explorer while JavaScript runs on all major browsers.
  • jQuery written in JavaScript
Performance:

Both jQuery and JavaScript have almost equal performance speed. jQuery has been tried in the course of recent years and it turned out to be quick and predictable.

advantage to adding jQuery:
  • Wide range of plugins available
  • It is light weight compared to other frameworks of JS
  • Easy to extend an active community
  • Uses simple and powerful syntax
JavaScript advantages and disadvantages:

Advantages:
  • Executed on the client side
  • Relatively fast to the end user
  • Uses extended functionality
Disadvantages:
  • Fights with security
  • Irregularity as far as usefulness and interface.
Jquery is a good framework for JavaScript, which is easily works with compatible browser. But for jQuery one should have proper understanding of JavaScript. It is simple and have many pre-made plugins and widgets. On the other hand JS is good for client side development using jQuery.

Featured Post

ADMEC Multimedia Institute Scholarship Program

The ADMEC Multimedia Institute scholarship program aims to select and trained talented non working female, married woman and non married m...