split() quirk

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

1. Christopher Benson-ManicaGuest

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

2. Fred OzGuest

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:

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

--
Fred

Fred Oz, Feb 25, 2005

3. Christopher Benson-ManicaGuest

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
4. ChrisGuest

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:
>
> var z = []; alert(z.length) // 0
> var z = [,]; alert(z.length) // 1
>
>
>
>
> --
> Fred

Chris, Feb 25, 2005
5. Dr John StocktonGuest

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.

--
<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
6. Fred OzGuest

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