Friday, April 25, 2014

Join array elements to a string

The join() method joins the array elements into a string.

Example:
<!DOCTYPE html>
<html>
<body onload="doit();">

<h1>mobile-web-app.blogspot.com</h1>
<p id="p1"></p>
<p id="p1a"></p>
<p id="p1b"></p>
<p id="p1c"></p>
<p id="p1d"></p>
<p id="p2"></p>
<p id="p3"></p>
 
<script type="text/javascript">
 
function doit()
{   var array = [1, 'a', 'BCD', 3.1416];

    document.getElementById("p1").innerHTML=array;
    
    document.getElementById("p1a").innerHTML=array[0] + ": " + typeof(array[0]);
    document.getElementById("p1b").innerHTML=array[1] + ": " + typeof(array[1]);
    document.getElementById("p1c").innerHTML=array[2] + ": " + typeof(array[2]);
    document.getElementById("p1d").innerHTML=array[3] + ": " + typeof(array[3]);
    
    var array2 = array.join('+');
    
    document.getElementById("p2").innerHTML="array.join('+'): " + array2 + ": " + typeof(array2);
    document.getElementById("p3").innerHTML="array.join(''): " + array.join('');
    
}
 
</script>
</body>
</html>



No comments:

Post a Comment