1

here is my javascript code:

var my_array = new Array();
my_array[0] = "a";
my_array[0] = "b";
my_array[0] = "c";

print_array(my_array);

function print_array(arr)
{
   alert(arr);
}

But it doesn't work, because I can't pass the array to the function. So, how can I pass an entire array to javascript function?

EDIT: Nevermind. I found the solution.
Hidden cause this is bad practice and passes the array as a string:

If someone will need to pass an array to the function, first use this: JSON.stringify() and javascript will be able to parse an array.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
user1745558
  • 91
  • 1
  • 1
  • 7

8 Answers8

9

The problem is you are assigning each element to index 0, so they are overwriting each other.

// should be
my_array[0] = "a";
my_array[1] = "b";
my_array[2] = "c";

// alternatively, if you don't want to track the index
my_array.push("a");
my_array.push("b");
my_array.push("c");

The rest of the code looks fine. You can pass whole arrays, or any object for that matter to a function in exactly the manner you have shown in your code.

When you alert it, javascript will concatenate the array into a comma separated string for easy viewing.

Billy Moon
  • 57,113
  • 24
  • 136
  • 237
  • 1
    or `my_array.push("a", "b", "c")` – roberkules Oct 27 '12 at 01:19
  • 1
    @roberkules: I like that one, I think you deserve a prize for that. – Billy Moon Oct 27 '12 at 01:23
  • @roberkules the problem with it is that it is less efficient, and harder to discern the meaning of. I do like to split strings by spaces in this way if there is a long list of options, or something that is easier to read as a unit, but only when efficiency is not important (which is actually most of the time for me in javascript). – Billy Moon Oct 27 '12 at 01:31
  • Maybe that's why you can see it often in libraries. There it's most likely only parsed once and then the array exists the whole time. So performance shouldn't be an issue. And if I use such an array definition I only parse it once. – roberkules Oct 27 '12 at 01:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18648/discussion-between-billy-moon-and-roberkules) – Billy Moon Oct 27 '12 at 01:41
  • Just posting the link from the chat - a better performance comparison: http://jsperf.com/array-vs-split-3 – roberkules Oct 27 '12 at 02:08
2

Try this code instead:

function print_array(arr)
{
   alert(arr);
}

var my_array = new Array();
my_array[0] = "a";
my_array[1] = "b";
my_array[2] = "c";

print_array(my_array);

You should define a function before you can call it as a good programming practice.

Also note the order of your elements that are being assigned, you're putting everything on 0, and they should be on 1, 2 etc.

jcolebrand
  • 15,889
  • 12
  • 75
  • 121
1

To extend Billy Moon's answer

You can define your array like this:

my_array = ["a", "b", "c"];

And like this:

my_array = [];
my_array[0] = "a";
// ...
Community
  • 1
  • 1
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
0
var my_array = ["a", "b", "c"];
// or a popular way to define (mainly string) arrays:
var my_array = "abc".split("");

print_array(my_array);

function print_array(arr) {
   alert(arr);
}
roberkules
  • 6,557
  • 2
  • 44
  • 52
0

You were using the same index 0

var my_array = new Array();
my_array[0] = "a";
my_array[1] = "b";
my_array[2] = "c";

JS Fiddle

codingbiz
  • 26,179
  • 8
  • 59
  • 96
0
var my_array = ["a","b","c"];


function print_array(arr)
{
   alert(arr);
}

print_array(my_array);
Fauzan DP
  • 51
  • 4
-2

Note this

function FollowMouse()
{
    var len = arguments.length;
    if(len == 0) return;
    //
    for(var i=0; i< len; i++)
    {
        arguments[i].style.top = event.clientY+"px";
        arguments[i].style.left = event.clientX+"px";
    }

};

//---------------------------

html page

<body onmousemove="FollowMouse(d1,d2,d3)">

<p><div id="d1" style="position: absolute;">Follow1</div></p>
<div id="d2" style="position: absolute;"><p>Follow2</p></div>
<div id="d3" style="position: absolute;"><p>Follow3</p></div>


</body>

can call function with any Args

<body onmousemove="FollowMouse(d1,d2)">

or

<body onmousemove="FollowMouse(d1)">
ali.b.y
  • 67
  • 2
-2
var f=d=>
  d.forEach(k=>
    alert(k)
  );
let a=[1,2];
f(a);

fiddle

Thielicious
  • 4,122
  • 2
  • 25
  • 35