SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Displaying elements of an array by using join() method

You have seen displaying elements of an array by looping through the array. Now we will see how to display elements of an array by using join method. Join() method is a built in method in JavaScript which takes a delimiter and create a string by adding all elements of the array by placing the delimiter between them.

This is another way to display all elements and if we give line break <br> as delimiter then all elements we can display vertically in any web page. We need not loop through by using length property.

We can directly display elements by using document.write or we can assign it to one string variable and then display.

var str=scripts.join(" : ");
document.write(str);



Here is the complete code for displaying elements of an array using join

<script type="text/javascript">
var scripts = new Array();
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
var str=scripts.join(" : ");
document.write(str);
//document.write(scripts.join(" <br> "));

</script>

The output of the above code is here

PHP : ASP : JavaScript : HTML

You can use line break <br> to generate a list by using the last line of the above code.

How to create array by splitting a string using split() method




Further readings
Declaring array in JavaScript
Displaying elements of an array by looping through
join():Displaying elements of an array using join() function
sort():Sorting of elements of an array using function
length:Length property to get the total number of elements in an array
reverse():Reversing the elements of an array
pop():Removeing last element of an array by using pop()
shift():Removeing first element of an array using shift()
push():Adding elements to array using push()
unshift():Adding elements to array using unshift()
splice():Add replace remove elements from an array using splice()
split():Creating array by splitting string variable
toString: To join all elements and create a string
concat: To join two or more arrays to a single array
slice: To return element from an array with starting and ending positions
Two Dimensional Array: Adding and displaying elements


nirmala04-10-2010
i need to know how to get user input and store it in array and use array functions in it
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked


Join Our Email List
Email:  
For Email Newsletters you can trust
Array Functions