Javascript Concat - Use Concat to Join Two Arrays in Javascript
i=new Array("Hi", "Hello");
j=new Array("Aloha", "Goodnight");
i=i.concat(j);
document.write(i);
Which Prints:
Hi,Hello,Aloha,Goodnight
-We
create an array and name it
i because we like one letter variable names.
-We create an array and name it
j because of our previously mentioned love of short names.
-We use Javascript's built-in
concat method on our
i array. In the parenthesis, we put what we want added to the end of our
i array. In this example, we want to addend the array we named
j.
-We use Javascript's built-in
document.write command to print to the browser.
NOTE: When we used
concat on our array
i we also stored the results right back into
i. We didn't have to. We could have stored the results into a new variable, if we had wanted to. We could have named our new array something clever like
k. (Then our code would have looked like the Copy-and-Paste code below.) That would have allowed our
i array to retain its original value.
NOTE: We could have put what we wanted addended to our first array straight into the
concat method, inside quotation marks, like so:
i=new Array("Hi", "Hello");
i=i.concat("Aloha,Goodnight");
document.write(i);
Which Prints:
Hi,Hello,Aloha,Goodnight
Free Copy and Paste Javascript Code:
<script type="text/javascript">
<!--
i=new Array("Hi", "Hello");
j=new Array("Aloha", "Goodnight");
k=i.concat(j);
document.write(k);
-->
</script>
Resources:
Javascript Array - Create an Array in Javascript
Javascript Join - Use Join to Make an Array into a String in Javascript
Javascript Splice - Use Splice to Replace Items in an Array in Javascript
Javascript Slice - Use Slice to Get Items from an Array in Javascript
Javascript Sort - Use Sort on Items in an Array in Javascript
Javascript Shift Use Shift to Remove Items from the Beginning of an Array in Javascript
Javascript Unshift - Use Unshift to Put Items on the Front of an Array in Javascript
Splitting an Array using Javascript
Javascript Push - Use Push on an Array in Javascript
PRINT to Browser with document.write Command in Javascript
JavaScript Kit - Nice Array Tutorial on Another Site
Javascript Alert
onClick Javascript
Javascript Referrer
Javascript Reset Form
Annoy Users with onBlur in Javascript
Get Query String Using Javascript
Use innerHTML to Change Text on a Page
Click HERE to comment or discuss at iLoveTheCode GOOGLE Group
| | HTML Gary B. Shelly, Th... | |
CSS David McFarland | | |
| | | |