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 element. Show all posts
Showing posts with label element. Show all posts

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: CharAt Function



charAt Function:
Using the charAt method you can find out the element in a string.

Syntax:
variablename.charAt(indexnumber);



Example in HEAD section:

<html>
<head>
<title>charAt Function</title>
<script language="javascript">
var anystring="Hello World";
document.write("The character at index 0 is " +anystring.charAt(0)+"<br>");
document.write("The character at index 1 is " +anystring.charAt(1)+"<br>");
document.write("The character at index 2 is " +anystring.charAt(2)+"<br>");
document.write("The character at index 3 is " +anystring.charAt(3)+"<br>");
document.write("The character at index 4 is " +anystring.charAt(4)+"<br>");
document.write("The character at index 5 is " +anystring.charAt(5)+"<br>");
document.write("The character at index 6 is " +anystring.charAt(6)+"<br>");
document.write("The character at index 7 is " +anystring.charAt(7)+"<br>");
document.write("The character at index 8 is " +anystring.charAt(8)+"<br>");
document.write("The character at index 9 is " +anystring.charAt(9)+"<br>");
document.write("The character at index 10 is " +anystring.charAt(10));
</script>
</head>
</html>


Example in BODY Section:

<html>
<head>
<title>charAt Function</title>
</head>
<body>
<script language="javascript">
var anystring="Hello World";
document.write("The character at index 0 is " +anystring.charAt(0)+"<br>");
document.write("The character at index 1 is " +anystring.charAt(1)+"<br>");
document.write("The character at index 2 is " +anystring.charAt(2)+"<br>");
document.write("The character at index 3 is " +anystring.charAt(3)+"<br>");
document.write("The character at index 4 is " +anystring.charAt(4)+"<br>");
document.write("The character at index 5 is " +anystring.charAt(5)+"<br>");
document.write("The character at index 6 is " +anystring.charAt(6)+"<br>");
document.write("The character at index 7 is " +anystring.charAt(7)+"<br>");
document.write("The character at index 8 is " +anystring.charAt(8)+"<br>");
document.write("The character at index 9 is " +anystring.charAt(9)+"<br>");
document.write("The character at index 10 is " +anystring.charAt(10));
</script>
</body>
</html>



OUTPUT

These lines display the following:

The character at index 0 is H
The character at index 1 is e
The character at index 2 is l
The character at index 3 is l
The character at index 4 is o
The character at index 5 is
The character at index 6 is W
The character at index 7 is o
The character at index 8 is r
The character at index 9 is l
The character at index 10 is d

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
Google