Here you can find a number of examples on Java Script.Please find the various kinds of examples listed on the right side of this site. If you don't find anything on Java Script then please write to us or leave a commnet. We are pleased to help and find the solution of your problem.
Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

JavaScript: Concat Function



concat Function:
Joins two arrays and returns a new array.

Syntax:
concat(arrayName2,arrayName3, ...,arrayNameN);



Example in HEAD section:

<html>
<head>
<title>Concat Function</title>
<script language="javascript">
var s1="Oh "
var s2="what a beautiful "
var s3="morning"
var s4=s1.concat(s2,s3)
document.write(s4);
</script>
</head>
</html>


Example in BODY section:

<html>
<head>
<title>Concat Function</title>
</head>
<body>
<script language="javascript">
var s1="Oh "
var s2="what a beautiful "
var s3="morning"
var s4=s1.concat(s2,s3)
document.write(s4);
</script>
</body>
</html>




OUTPUT

Oh what a beautiful morning

JavaScript: unshift Function



unshift Function:
Adds one or more elements to the beginning of an array and returns the new length of the array.

Syntax:
arrayname.unshift(element1,element2.........elementn)



Example in HEAD section:

<html>
<head>
<title>unshift Function</title>
<script language="javascript">
var nushifted;
myArray = ["angel", "clown"];
document.writeln("myArray before: " + myArray + "<p>");
unshifted = myArray.unshift("mandarin", "surgeon");
document.writeln("myArray after: " + myArray + "<p>");
</script>
</head>
</html>


Example in BODY section:

<html>
<head>
<title>unshift Function</title>
</head>
<body>
<script language="javascript">
var nushifted;
myArray = ["angel", "clown"];
document.writeln("myArray before: " + myArray + "<p>");
unshifted = myArray.unshift("mandarin", "surgeon");
document.writeln("myArray after: " + myArray + "<p>");
</script>
</body>
</html>




OUTPUT

myArray before: angel,clown

myArray after: angel,clown,mandarin,surgeon

JavaScript: Shift Function



shift Function:
Removes the first element from an array and returns that element. This method changes the length of the array.

Syntax:
arrayname.shift()



Example in HEAD section:

<html>
<head>
<title>Shift Function</title>
<script language="javascript">
myArray = ["angel", "clown", "mandarin", "surgeon"];
document.writeln("myArray before: " + myArray + "<p>");
shifted = myArray.shift();
document.writeln("myArray after: " + myArray + "<p>");
document.writeln("Removed this element: " + shifted);
</script>
</head>
</html>


Example in BODY section:

<html>
<head>
<title>Shift Function</title>
</head>
<body>
<script language="javascript">
myArray = ["angel", "clown", "mandarin", "surgeon"];
document.writeln("myArray before: " + myArray + "<p>");
shifted = myArray.shift();
document.writeln("myArray after: " + myArray + "<p>");
document.writeln("Removed this element: " + shifted);
</script>
</body>
</html>




OUTPUT

myArray before: angel,clown,mandarin,surgeon

myArray after: clown,mandarin,surgeon

Removed this element: angel

Javascript: Create Array



Arrays:Arrays are Javascript object data capable of story sequence of values. These values are store index location with in array.
Array must be declare before it use.

Syntax:
1. arrayname = new Array(arraylength);
2. arrayname = new Array();

An array element index start with Zero (0) and end with arraylength-1



Example:

<html>
<head>
<title>Arrays</title>
</head>
<body>

&ltscript language="javascript">
var months=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var i=0;
for(i=0;i<12;i++)
{
document.write(months[ i ] + "&ltbr>");
}
</script>

</body>
</html>



OUTPUT

January
February
March
April
May
June
July
August
September
October
November
December

Google