www.webdeveloper.com
Results 1 to 2 of 2

Thread: when 0 isn't 0

  1. #1
    Join Date
    Apr 2009
    Posts
    33

    when 0 isn't 0

    I've never noticed this before but

    Code:
    var ray = new Array('one', 'two', 'three');
    for (i in ray) {
        if (i) alert (ray[i]+" "+i);
    }
    I get an alert, "one 0", whereas

    Code:
    var x = 0;
    if (x) alert("hello")
    I don't, which is what I would expect. Why is zero counted as true in the first case?

  2. #2
    Join Date
    Apr 2009
    Posts
    33
    Ah,

    Code:
    for (i in ray) alert(typeof(i))
    ...it's a string :/

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles