Category Archive Javascript

Bynayakdillip

How to Reverse an Array

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

Example:-

[html]
var programming = ["c#", "c","java","apex"];
var programmingReverseOrder = programming.reverse();
alert(programmingReverseOrder);
[/html]
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:-
[html]
var programming = ["c#", "c","java","apex"];
var dataBaseRelated = ["SQL", "SOQL", "MySQL"];
var application = programming.concat(dataBaseRelated);
alert(application);
[/html]
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:-

[html]
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);
[/html]
//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:-
[html]
var languageArray=[‘c#’,’c’,’java’];
//its diplay like c#,c,java
alert(languageArray);

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

//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:-

[html]
//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);
[/html]
//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

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

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

when you use var

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

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:
[html]
‘use strict’;
let i= 3;
let i = 5; // SyntaxError: Identifier ‘i’ has already been declared
[/html]

[html]
‘use strict’;
var i = 3;
var i = 5; // No problem, `i` is replaced.
[/html]

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:
[html]
var arrayOFLanguages=["","","",….];//var dynamic type it holds different data type like Integer,String,Char,List etc
[/html]

Example:
[html]
var arrayOfRollNumbers=[1,2,3,4,5,6];//it store integer value of roll numbers
[/html]

[html]
Var arrayOfNames=[‘Dillip’,’Kunal’];//It store string value of Names
[/html]

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

Methods in Array

Push
Pop
Length
Concat
Sort
Reverse
Delete
ToString