5

I have an array

var arr = [1,2,3,4,5,6,7,8,9,10];

How to display all items of the array using an alert box?

I have tried : alert(arr); and it shows nothing.

Edit: I want display this array like php print_r function.

 output needed like: array["key" => "value", "key" => "value", ...];
5
  • 1
    alert(JSON.stringify(arr)) or alert(arr.join(" ")) Commented Sep 9, 2015 at 6:43
  • var arr = [1,2,3,4,5,6,7,8,9,10]; alert(arr[1]); Commented Sep 9, 2015 at 6:43
  • 5
    If you're just trying to debug, I would HIGHLY suggest ditching alert and using console.log() Commented Sep 9, 2015 at 6:51
  • 1
    @flcoder: I'm indeed wondering why console.log() hasn't been suggested from the very beginning. Commented Sep 9, 2015 at 6:54
  • thanks to all... its working but by using this: alert(arr.toString()); alert(arr.join(", ")); .... explain ? Commented Sep 9, 2015 at 7:01

7 Answers 7

4

You could also use the JavaScript function toString().

alert(arr.toString());
Sign up to request clarification or add additional context in comments.

1 Comment

Also, instead of alert(), I used console.log() in my answer, but it got removed by an editor. Console.log prints to your js console (usually accessed by pressing ctrl+shift+J in your browser)
3

To show them in csv, you can use .join(",") along with array object:

alert(arr.join(", "));

for printing individually:

$.each(arr, function( index, value ) {
  alert( value );
})

Comments

2

As I'm wondering why console.log() hasn't been provided as an answer, here it is.

Do:

console.log(arr);

And open the developper toolbar (F12 on most browsers) and go to the console tab. You should be able to see and expand your array.

1 Comment

Nevermind, just saw the requirement in OP's question ("How to display all item of array by alert box ?"). I'll leave my answer though (the OP might not be aware at all of this feature).
1
var arr = [1,2,3,4,5,6,7,8,9,10];
alert(arr);
for(var i = 0 ; i < arr.length; i++){
alert("key "+ i + " and " + "Value is "+arr[i]);
}

FIDDLE

To alert each value use this

Comments

1

var a = {
  "1": 15,
  "2": 16,
  "3": 17,
}

console.log(a);

Comments

0
    var arr = [1,2,3,4,5,6,7,8,9,10];
    var arrstr="arr[";
    for(var i=0;i<arr.length;i++){
        arrstr+="\""+i+"\" : \""+arr[i]+"\"";   //you can change ":" for "=>", if you like
        if(i!=arr.length-1){//if not the last one ,add "," 
            arrstr+=",";    
        }else{
            arrstr+="]";
        }
    }

    alert(arrstr);

Comments

-1
for(var i = 0; i < teams.length; i++) {
        console.log(teams[i]);
           
}

worked for me.

2 Comments

Not sure if this is a "thanks" or an attempt at an answer. It doesn't actually answer the original problem, as it doesn't use alert() or provide the format that the OP asked for.
@jmoerdyk Looks legit (however, not particularly good) to me. I think the usage of console.log() comes from this comment on the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.