Latest News

Array Functions in Javascript,Array Functions,Functions in Java Script



var a=new.Array(10,20,30,40);
for(i=0;i<3;i++)
{
document.write(a[i]=”<br>”);
}

Associative array: If any array has specific indexes then that array is called associative array.
Associative array cannot print using standard loops, we have to use in loop to print the array.
Syntax of For-In:
for(variable in array)
{
array[variable];
}
Example: var b=new.Array();
b[‘age’]=25;
b[‘name’]=’+a+’;
b[‘salary’]=2500;
for(x in b)
{
document.write(x+’———-‘+b[x]+’<br>’);
}

Array build in functions:
Length: It written number of elements in the array.
Syntax: arrayname.length
Example:
var a =new.Array(10,20,30,40);
document.write(a.length);
Push: It is used to insert a new element in existing value.
Syntax: array name.push
Pop: It is used to delete the last element in the array.
Syntax: array name.pop
Example:
a.pop();
for(x in a)
{
document.write(x+’—-‘+a[x]+’<br>’);
}
NOTE: push and pop functions follow LIFO rule, these functions will work only last index of the array.
Unshift: It is used to insert a value in existing array, but it insert in first index.
Syntax: arrayname.unshift
Shift: It is used to delete first value in the array.
Syntax: array name.shift
NOTE: shift and unshift functions follow FIFO rule, so it works only on first index of the array.
ConCat: It is used to combine 2 or more arrays.
Syntax: array name.ConCat()
 Sort: It is used to sort the array, sorting order is numeric, upper alfa anf lower alfa.
Syntax: array name.sort();
NOTE: sorting function will work depends on first character of value.
 Reverse: It prints the array in reverse order.
Syntax: array name.reverse;
 Split: It is used to convert string to array.
Syntax: string.split of seperato
example:
var str=”this is a sample string”;
var z=str.split(‘ ‘);
for(x in z)
{
document.write(x+’—‘+z[x]+’<br>’);
}

Join: It is used to convert array to string, replace cama(,) with specified separator.
Syntax: array.join(separator);
Example:
var str2=z.join(‘ ‘);
document.write(str2);

No comments:

Post a Comment

eWeb Classes Powered by MV Innovative InfoTech Copyright © 2014

Theme images by Bim. Powered by Blogger.