Arrays and JavaScript

+1 Robert Summers · October 19, 2014
Okay so is this possible with JavaScript to print Arrays in functions and write them out like this


function printArray(){
var my_array = new Array(14, 354, 3, 21, 56, 242);
}

document.write("My array = " + printArray(my_array) + " and the length of my array is " + my_array.length() + " characters long<hr/>");


If so what am I doing wrong, many thanks in return

Kind regards 
Robert

Post a Reply

Replies

Oldest  Newest  Rating
0 Phillip Drake · October 20, 2014
Robert, you can use the function as you have listed with a little adjustment.  Keep in mind that if you declare a variable inside a function, it is only available locally inside that function.  So, here is the way it should be written:


<script>
    function printArray(){
var my_array = new Array(14, 354, 3, 21, 56, 242);
document.write("My array = " + my_array + " and the length of my array is " + my_array.length + " items long.<hr>");
}
    printArray();
</script>
  • 1

Javascript

106,979 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators