JavaScript
arrays are used to store multiple values in a single variable.
Array
Syntax
var
array-name = [item1,
item2,
...];
var
colors = ["Red","Green","Black"];
Methods
of Array
Methods of
Array
- Pop
The pop()
method removes the last value of an array permanently will return the
remaining values.
Example:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.pop());
alert(mdArr);
Result:5 and
5,6,7,8,9
- Push
The push()
method adds the new value in the array at the end and will return the
new set of array values.
Example:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.push(6,8,9));
alert(mdArr);
Result:9 and
5,6,7,8,9,5,6,8,9
- Shift
The shift()
method is just like pop method. The only difference is it will remove
the first value from an array permanently and return the remaining
values.
Example:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.shift());
alert(mdArr);
Result:5 and
6,7,8,9,5
- Unshift
The
unshift() method is the opposite of shift() method. The only
difference is it will add the new values at the beginning of an array
and return the new set of array values.
Example:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.unshift(3,2,1));
alert(mdArr);
Result: 9
and 3,2,1,5,6,7,8,9,5
- indexOf()
It will
display the index of a given array element. If a number is appearing
twice in an array, then it will return the index of first occurrence
of a given number.
Example:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.indexOf(5));
Result: 0
For Invalid
value:
Example:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.lastIndexOf(10));
Result: -1
- lastIndexOf()
It will
display the index of a given array element. If a number is appearing
twice in an array, then it will return the index of last occurrence
of a given number.
Example:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.lastIndexOf(5));
Result: 5
For Invalid
value:
Example:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.lastIndexOf(10));
Result: -1
- join()
It will join
all array elements into a string specified by a separator.
Example:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.join(‘’));//no
space
Result:
56785
Example:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.join(‘
’));//added a space
Result: 5 6
7 8 5
Example:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.join(‘*’));
Result:5*6*7*8*5
- slice()
Syntax:
slice(Begin Index, End Index)
Begin index
will take the position of a given array element and will display
values
depending upon the End Index value given. Last index value will not
be included.
Example:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.slice(2,
4));
Result: 7,8
It will
take values from 2nd index i.e. 7 and will
return values till 3rd index. 4th
index value will not be included.
For Invalid
values:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.splice(8,
3));
Result:
Blank Alert
- splice()
Syntax:
slice(Begin Index, No.of values)
Begin index
will take the position of a given array element and will display
values
depending upon the value given for No.of values given.
Example:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.splice(2,
4));
Result:
7,8,9,5
It will
take values from the 2nd index i.e. 7 and
will return upto 4 values.
For Invalid
values:
var mdArr =
[5,6,7,8,9,5];
alert(mdArr.splice(8,
3));
Result:
Blank Alert
No comments:
Post a Comment