Monthly Archive February 2019

Bynayakdillip

JavaScript if else and else if

Conditional Statement are used to perform different actions based on different condition

Expression
if(condition){
       block of code
   }else if(condition){
    block of code
  }else{
 	block of code
 }

shipping.html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Country Shipping</title>
</head>

<body>
    <div id="shippingInfoId" />
</body>
<script type="text/javascript" src="shipping.js">

</html>
shipping.js
var countryName = 'USA';
if (countryName == 'USA') {
    document.getElementById('shippingInfoId').innerText = countryName + '--' + '2-day Shipping';
} else if (countryName == 'Canda') {
    document.getElementById('shippingInfoId').innerText = countryName + '--' + '3-day Shipping';
} else if (countryName == 'India') {
    document.getElementById('shippingInfoId').innerText = countryName + '--' + '1-day Shipping';
}
else {
    document.getElementById('shippingInfoId').innerText = 'Other' + '--' + '5-day Shipping';
}


Bynayakdillip

Switch Case in JavaScript

The switch case statement is a multiway  branch statement. it provides  an easy way to dispatch execution to different parts of code.

Switch case statement in JavaScript is also used for decision making purposes.

 switch (expression)
 {
    case value1:
        statement1;
        break;
    case value2:
        statement2;
        break;
 	   .
	    .
    	case valueN:
        statementN;
        break;
    default:
        statementDefault;
}

Example switchcase.js
var customerCountry='AUS';
switch (customerCountry) {
    case 'USA':
        document.getElementById('shippingInfo').innerText=customerCountry+'2-day Shipping';
        break;
    case 'Canda':
        document.getElementById('shippingInfo').innerText=customerCountry+'3-day Shipping';
        break;
    case 'INDIA':
        document.getElementById('shippingInfo').innerText=customerCountry+'--->'+'1-day Shipping';
        break;
    default:
        document.getElementById('shippingInfo').innerText=customerCountry+'5-day Shipping';
        break;
}
switchCase.Html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Country Shipping</title>
</head>

<body>
    <div id="shippingInfoId" />
</body>
<script type="text/javascript" src="switchCase.js">

</html>