reverse ()
JavaScript array reverse() method reverses the element of an array. The first array
element becomes the last and the last becomes the first.
Syntax
Its syntax is as follows:
array.reverse();
Return Value
Returns the reversed single value of the array.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array reverse
Method</title>
</head>
<body>
<script
type="text/javascript">
var arr = [0, 1, 2, 3].reverse();
document.write("Reversed array is :
" + arr );
</script>
</body>
</html>
Output
Reversed array is : 3,2,1,0
shift ()
Javascript array shift() method removes the first element from an array and returns
that element.
Syntax
Its syntax is as follows:
array.shift();
Return Value
Returns the removed single value of the array.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array shift Method</title>
</head>
<body>
<script
type="text/javascript">
var element = [105, 1, 2,
3].shift(); document.write("Removed element is : " + element );
</script>
</body>
</html>
Output
Removed element is : 105
slice ()
Javascript array slice() method extracts a section of an array and returns a new
array.
Syntax
Its syntax is as follows:
array.slice( begin [,end] );
Parameter Details
·
begin : Zero-based index at
which to begin extraction. As a negative
index, start indicates an offset from the end of the sequence.
·
end : Zero-based index at which to end extraction.
Return Value
Returns the extracted array based on the passed parameters.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array slice
Method</title>
</head>
<body>
<script
type="text/javascript">
var arr = ["orange",
"mango", "banana", "sugar", "tea"];
document.write("arr.slice( 1, 2) : " + arr.slice( 1, 2) );
document.write("<br />arr.slice( 1, 2) : " + arr.slice( 1, 3) );
</script>
</body>
</html>
Output
arr.slice( 1, 2) : mango
arr.slice( 1, 2) : mango,banana
some ()
Javascript array some() method tests whether some element in the array passes the
test implemented by the provided function.
Syntax
Its syntax is as follows:
array.some(callback[, thisObject]);
Parameter Details
·
callback : Function to test for each element.
·
thisObject : Object to use as this when
executing callback.
Return Value
If some element pass the test, then it returns true, otherwise false.
Compatibility
This method is a
JavaScript extension to the ECMA-262 standard; as such it may not be present in
other implementations of the standard. To make it work, you need to add the
following code at the top of your script.
if (!Array.prototype.some)
{
Array.prototype.some = function(fun /*,
thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this &&
fun.call(thisp, this[i], i, this))
return true;
}
return false;
};
}
Example
Try the following example.
<html>
<head>
<title>JavaScript Array some
Method</title>
</head>
<body>
<script
type="text/javascript">
if (!Array.prototype.some)
{
Array.prototype.some = function(fun /*,
thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this &&
fun.call(thisp, this[i], i, this))
return true;
}
return false;
};
}
function isBigEnough(element, index, array) {
return (element >= 10);
}
var retval = [2, 5, 8, 1,
4].some(isBigEnough); document.write("Returned value is : " + retval
);
var retval = [12, 5, 8, 1,
4].some(isBigEnough);
document.write("<br />Returned
value is : " + retval );
</script>
</body>
</html>
Output
Returned value is : false
Returned value is : true
sort ()
Javascript array sort()
method sorts the elements of an array.
Syntax
Its syntax is as follows:
array.sort( compareFunction );
Parameter Details
compareFunction:
Specifies
a function that defines the sort order. If omitted, the array is sorted lexicographically.
Return Value
Returns a sorted array.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array sort
Method</title>
</head>
<body>
<script
type="text/javascript">
var arr = new Array("orange",
"mango", "banana", "sugar");
var sorted = arr.sort();
document.write("Returned string is :
" + sorted );
</script>
</body>
</html>
Output
Returned string is :
banana,mango,orange,sugar
splice ()
Javascript array splice() method changes the content of an array, adding new
elements while removing old elements.
Syntax
Its syntax is as follows:
array.splice(index, howMany, [element1][,
..., elementN]);
Parameter Details
·
index: Index at which to start changing the array.
·
howMany: An integer indicating
the number of old array elements to remove.
If howMany is 0, no elements are
removed.
·
element1, ..., elementN: The elements to add
to the array. If you don't specify
any elements, splice simply removes the elements from the array.
Return Value
Returns the extracted array based on the passed parameters.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array splice
Method</title>
</head>
<body>
<script
type="text/javascript">
var arr = ["orange",
"mango", "banana", "sugar", "tea"];
var removed = arr.splice(2, 0,
"water"); document.write("After adding 1: " + arr );
document.write("<br />removed is: " + removed);
removed = arr.splice(3, 1);
document.write("<br />After adding
1: " + arr );
document.write("<br />removed is:
" + removed);
</script>
</body>
</html>
Output
After adding 1:
orange,mango,water,banana,sugar,tea removed is:
After adding 1: orange,mango,water,sugar,tea
removed is: banana
toString ()
Javascript array toString() method returns a string representing the source code of
the specified array and its elements.
Syntax
Its syntax is as follows:
array.toString( );
Return Value
Returns a string representing the array.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array toString
Method</title>
</head>
<body>
<script
type="text/javascript">
var arr = new Array("orange",
"mango", "banana", "sugar");
var str = arr.toString();
document.write("Returned string is : " + str );
</script>
</body>
</html>
Output
Returned string is :
orange,mango,banana,sugar
unshift ()
Javascript array unshift() method adds one or more elements to the beginning of an
array and returns the new length of the array.
Syntax
Its syntax is as follows:
array.unshift( element1, ..., elementN );
Parameter Details
element1, ..., elementN
: The elements to add
to the front of the array.
Return Value
Returns the length of the new array. It returns undefined in IE browser.
Example
Try the following example.
<html>
<head>
<title>JavaScript Array unshift
Method</title>
</head>
<body>
<script
type="text/javascript">
var arr = new Array("orange",
"mango", "banana", "sugar");
var length =
arr.unshift("water"); document.write("Returned array is : "
+ arr ); document.write("<br /> Length of the array is : " +
length );
</script>
</body>
</html>
Output
Returned array is :
water,orange,mango,banana,sugar
Length of the array is : 5
0 comments:
Post a Comment