Category Archive Javascript

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>

Bynayakdillip

How to Reverse an Array

The reverse() method reverses the elements in an array.
Syntax:arrayName.reverse()

Example:-

var programming = ["c#", "c","java","apex"];
var programmingReverseOrder = programming.reverse();
alert(programmingReverseOrder);

it will be print like
apex,java,c,c#

Bynayakdillip

How to join or combine two arrays to one array?

JavaScript Array concat() Method

it returns combine of records from two arrays.

Example:-

var programming = ["c#", "c","java","apex"];
var dataBaseRelated = ["SQL", "SOQL", "MySQL"];
var application = programming.concat(dataBaseRelated);
alert(application);

It will print like this
c#,c,java,apex,SQL,SOQL,MySQL

Bynayakdillip

How to get size of items in an array?

JavaScript Array length Property
length:it returns size(no.of items ) of an array

Example:-

var languageArray=['c#','c','java'];
//its diplay like c#,c,java
alert(languageArray);

//remove last item using pop method
var arraylength=languageArray.length;
alert(arraylength);

//it will print 3(no of items in languageArray is 3)

Bynayakdillip

How to remove last item to an array?

JavaScript Array Pop() Method

Pop(): it helps to remove last item(element) from an array and returns the final array.

Example:-

var languageArray=['c#','c','java'];
//its diplay like c#,c,java
alert(languageArray);

//remove last item using pop method
languageArray.pop();
alert(languageArray);

//it will print c#,c,java

Bynayakdillip

How to append item to an array?

JavaScript Array push() Method

Push():-Its helps to add a new item at the end of the array.use the push() method/function to append an array

Example:-

//Array of programing language
var languageArray=['c#','c','java'];
//its diplay like c#,c,java
alert(languageArray);

//Append new value to the array

languageArray.push('salesforce');

alert(languageArray);

//it will print c,c#,java,Salesforce

Note: You also push object type in an array its depend upon your requirement.

Bynayakdillip

Var and Let in Javascript

Var and Let in Javascript

Var:-Dynamic data type in javascript
let:-dynamic data type in javscript

Difference between Var and Let
The difference is “let” is visible only in the for() loop and var is visible to the whole function.

when you use Let

<script>
var iPLTemsList = ['CSK', 'DD', 'KXIP', 'KKR','MI','RCB','SRH','RR'];
for(let ipl in iPLTemsList){
  alert(iPLTemsList[ipl]);
}
alert(ipl);
</script>

//JavaScript error: Uncaught ReferenceError: ipl is not defined on line 6

when you use var

<script>
var iPLTemsList = ['CSK', 'DD', 'KXIP', 'KKR','MI','RCB','SRH','RR'];
for(Var ipl in iPLTemsList){
  alert(iPLTemsList[ipl]);
}
alert(ipl);//Display 7
</script>

Also, If you Redeclaration var and let, var will let you re-declare the same variable in the same scope. On the other hand, let will not:

'use strict';
let i= 3;
let i = 5; // SyntaxError: Identifier 'i' has already been declared
'use strict';
var i = 3;
var i = 5; // No problem, `i` is replaced.

Advantages of using let over var within a function. the difference is that let is block-scoped and var is not.

Note:-Experts always recommend solely using let everywhere, if it is available in your environment.

Bynayakdillip

JavaScript Arrays

Before learning Javascript Array.

Why did you learn javascript?
Javascript has become an essential web technology along with HTML and CSS.You must Learn javascript if you want to get into web development with using Any Technology (.Net MVC, PHP, Java, Salesforce, Dynamic CRM)

let’s discuss something about javascript Array.

An array is a collection of same type data(means integer, string etc) values.we can store many values and retrieve values as per index. Also, you can store lists an array.

How to create arrays

Synatax:

var arrayOFLanguages=["","","",....];//var dynamic type it holds different data type like Integer,String,Char,List etc

Example:

var arrayOfRollNumbers=[1,2,3,4,5,6];//it store integer value of roll numbers
Var arrayOfNames=['Dillip','Kunal'];//It store string value of Names

For Accessing Particular Value

Var rollNumber=arrayOfRollNumbers=[0];//it display 1 
var name=arrayOfNames[1];//it display kunal(Retrive Records by index.Index always start with 0.
)

Methods in Array

Push
Pop
Length
Concat
Sort
Reverse
Delete
ToString