WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1 Javascript - Pop, Push, Reverse, Join methods on Wed May 09, 2012 4:56 pm

Mr.Joker

avatar
Mr. WebArtz
Mr. WebArtz
Hello once more, I am Mr.Joker and in this tutorial you will learn very important methods as Pop, Push, Reverse, Join and Sort. Lets begin:

-Pop : With pop method you can delete the last "member" or Array. So if array is like (1,2,3,4,5) and you use pop , number 5 will be deleted. Here is example:

Code:
<script type="text/javascript">
var x = new Array(1,2,3,4,5);
x.pop()
document.write(x)
</script>

-Push : With push method you can add another element inside array. You can do it on simple way like this:

Code:
<script type="text/javascript">
var x = new Array(1,2,3,4,5);
x.push(6)
document.write(x)
</script>
Now you will have 6 included into this Array, but what if you need to add more Arrays by user choise? Here is a code:

Code:
<script type="text/javascript">
var x = new Array(1,2,3,4,5);
for (i=0;i<3;i++) {
var question = new Array()
question[i] = window.prompt("Enter a number to add in Array:")
x.push(question[i])
}
document.write(x)
</script>
Its simple to understand. First we had to make variable question as Array so we can store many values inside it. Then, because question variable is allerady inside for loop we use that to store in order those values that user enter via promptbox. Then after that we have to store those values inside x variable. We do that with push method including inside brackets what we include, and we include answers form question in order.
-Reverse : With reverse method we can reverse order inside Array so that mean that last element will be first now, and first will be last and so on. Example:
Code:
<script type="text/javascript">
var x = new Array(1,2,3,4,5);
x.reverse()
document.write(x)
</script>
Again really simple method. Just type name of variable add dot and type reverse with brackets.
-Join : With join method you can join all elelments of array with some sign. For example:
Code:
<script type="text/javascript">
var x = new Array(1,2,3,4,5);
var y = x.join("-")
document.write(y)
</script>
We have now two variables, first one is normal Array and second one is first one with joined elements via "-" sign. That will look like this:
1-2-3-4-5

-Sort : With sort method you can sort all elements of Array by alphabetic order. For example:
Code:
<script type="text/javascript">
var x = new Array("Devil","Test","Angel")
var y = x.sort()
document.write(y)
</script>
We have an array with elements and some variable y that sort elements in alphabetic order. When you run script you will see that Angel is first, Devil is second and you assume next.

Thats all in this tutorial, hope you like it, you can always ask if you need help.



Last edited by Mr.Joker on Thu May 10, 2012 5:13 am; edited 1 time in total

RSguideMaker

avatar
Registered Member
Registered Member
Good tutorial

Mr.Joker

avatar
Mr. WebArtz
Mr. WebArtz
Updated with sort method, because I forgot to add it Very Happy

Sponsored content


View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum