joinThe join function is a pretty simple concept to work out. It does exactly what it is called and that is it joins strings. (Strings are text, words, sentences all of that) So, I'll start you off with a basic example, and then show you a variation using another variable in the mix.
Let's get started:
<script type='text/javascript'>
var rawr = new Array();
rawr[0] = "Me";
rawr[1] = "like";
rawr[2] = "to";
rawr[3] = "code.";
var tcz = rawr.join(" ")
document.write(tcz);
</script>
Ok, let's go through this step by step.
var rawr = new Array();
We have defined the variable
rawr as our array. An array is a set of variables stored into one variable. I like the to think it of like this; the data in the variable is your letter, the variable is our envelope and your array variable is your post office.
rawr[0] = "Me";
rawr[1] = "like";
rawr[2] = "to";
rawr[3] = "code.";
So, we've stored a bunch of plain text words in our array and that set's up for our next step, joining them all into one sentence.
var tcz = rawr.join(" ")
This is where our join() function comes into play. We are defining
tcz as our variavle to hold our data (the data being joined). So,
tcz is going to take the variable from our array
rawr and join them into one string. What is between the quotes in the brackets in our join function is what will separate our variables in our new string. In this example, I've just added a space so it shows up like a sentence, but you can always use something different, even <br /> works in there. You could also just leave it as join(), but the default separator is a comma, so it all depends on what you're using this for.
document.write(tcz);
Last but not least, we will output everything in our document.write(); function. We are writing the joint variables from our rawr array on our page, and believe it or not, that's it. Our outpue will now look like this
Me like to code.
That about sums up the join() function, but here's another example to show you how it can be implemented even in your IF coding.
<script>
var url = location.href.split("?");
var rawr = new Array();
rawr[0] = url[0];
rawr[1] = url[1];
rawr[2] = "hey";
tcz = frig.join("<br />")
document.write(tcz);
</script>
This follows the same procedure as the code above, only this time we are joining a split url. To learn more about the split() function, check out
my tutorial and take a look at
slayer's as well.
So, we are now joining the split url from our variable
url, adding the word hey at the end and this time we're separating them with the <br /> tag so it will show up with one piece of text per line. Our output will look like this now
Wherever your location is part one from where you split it
Wherever your location is part two from where you split it
hey
Well, I hope this tutorial helped you out or at least refreshed you on join(). Enjoy!