split() quirk

Discussion in 'Javascript' started by Christopher Benson-Manica, Feb 25, 2005.

  1. Why does ''.split(',') yield an array with length 1 rather than an
    array with length 0?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Feb 25, 2005
    #1
    1. Advertising

  2. Christopher Benson-Manica

    Fred Oz Guest

    Christopher Benson-Manica wrote:
    > Why does ''.split(',') yield an array with length 1 rather than an
    > array with length 0?
    >


    Because the first element of the array will be everything up to
    the first ','. You have created an array with one element that
    contains nothing. It is equivalent to:

    var newArray = [,];

    Which has a length of 1, but it contains nothing.


    Some examples:

    alert( ''.split(',').length) // 1
    var z = []; alert(z.length) // 0
    var z = [,]; alert(z.length) // 1




    --
    Fred
    Fred Oz, Feb 25, 2005
    #2
    1. Advertising

  3. Fred Oz <> spoke thus:

    > Because the first element of the array will be everything up to
    > the first ','. You have created an array with one element that
    > contains nothing. It is equivalent to:


    I'm sure it's just me, but it seems counterintuitive to me :) Thanks.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Feb 25, 2005
    #3
  4. Christopher Benson-Manica

    Chris Guest

    var z = [,]; alert(z.length) // 1

    returns 2 for me, is it that there's a space before and after the comma?

    Chris

    "Fred Oz" <> wrote in message
    news:421f4676$0$30570$...
    > Christopher Benson-Manica wrote:
    > > Why does ''.split(',') yield an array with length 1 rather than an
    > > array with length 0?
    > >

    >
    > Because the first element of the array will be everything up to
    > the first ','. You have created an array with one element that
    > contains nothing. It is equivalent to:
    >
    > var newArray = [,];
    >
    > Which has a length of 1, but it contains nothing.
    >
    >
    > Some examples:
    >
    > alert( ''.split(',').length) // 1
    > var z = []; alert(z.length) // 0
    > var z = [,]; alert(z.length) // 1
    >
    >
    >
    >
    > --
    > Fred
    Chris, Feb 25, 2005
    #4
  5. JRS: In article <cvndi4$2ce$>, dated Fri, 25 Feb 2005
    14:43:16, seen in news:comp.lang.javascript, Christopher Benson-Manica
    <> posted :

    >Why does ''.split(',') yield an array with length 1 rather than an
    >array with length 0?


    I have a vague recollection that the result may be browser-dependent.

    But what you (and I) get agrees with ECMA 262, Edn 3, 15.5.4.14, para 3,
    IMHO.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3 Turnpike 4 ©
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
    <URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm> clpdmFAQ;
    <URL:http://www.borland.com/newsgroups/guide.html> news:borland.* Guidelines
    Dr John Stockton, Feb 25, 2005
    #5
  6. Christopher Benson-Manica

    Fred Oz Guest

    Chris wrote:
    > var z = [,]; alert(z.length) // 1
    >
    > returns 2 for me, is it that there's a space before and after the comma?



    No, it's whether you use IE or not. Firefox/Mozilla/Safari all
    give 1, but IE gives 2. :)

    --
    Fred
    Fred Oz, Feb 25, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Morris

    Re: dtd and quirk mode

    Chris Morris, Jul 22, 2003, in forum: HTML
    Replies:
    0
    Views:
    381
    Chris Morris
    Jul 22, 2003
  2. Richard
    Replies:
    1
    Views:
    398
  3. Blinky the Shark

    Konqueror Quirk? Safari?

    Blinky the Shark, Feb 18, 2004, in forum: HTML
    Replies:
    9
    Views:
    427
    Blinky the Shark
    Feb 20, 2004
  4. Spartanicus

    IE6 quirk check

    Spartanicus, Jul 16, 2004, in forum: HTML
    Replies:
    2
    Views:
    433
    Spartanicus
    Jul 16, 2004
  5. David Graham

    quirk or standard mode

    David Graham, Dec 21, 2005, in forum: HTML
    Replies:
    2
    Views:
    750
    David Graham
    Dec 21, 2005
Loading...

Share This Page