Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #12524 > unrolled thread

Integer vs. string

Started byTim Streater <timstreater@greenbee.net>
First post2012-03-09 13:49 +0000
Last post2012-03-10 21:06 +0000
Articles 6 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  Integer vs. string Tim Streater <timstreater@greenbee.net> - 2012-03-09 13:49 +0000
    Re: Integer vs. string "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-09 18:59 +0000
      Re: Integer vs. string Tim Streater <timstreater@greenbee.net> - 2012-03-09 21:47 +0000
    Re: Integer vs. string RobG <rgqld@iinet.net.au> - 2012-03-10 14:56 -0800
      Re: Integer vs. string Tim Streater <timstreater@greenbee.net> - 2012-03-10 23:14 +0000
    Re: Integer vs. string Dr J R Stockton <reply1210@merlyn.demon.co.uk> - 2012-03-10 21:06 +0000

#12524 — Integer vs. string

FromTim Streater <timstreater@greenbee.net>
Date2012-03-09 13:49 +0000
SubjectInteger vs. string
Message-ID<timstreater-677053.13490009032012@news.individual.net>
I got burnt today by doing:

  var a = b + 1;

Now b is supposed to be an integer, but for some reason at the time in 
question, it was treated as a string.

The variable b is declared globally but not initialised there. During 
its life it is initialised to the length of an array (as in 
myarray.length). It's operated on as follows:

b--;

x = b - 1;
// some stuff done here
b = x;

b = -1;

b = 0;

It may also be set to the index of a simple for-loop such as:

for (x=0; x<max; x++)
   {
   // Some code including break;
   }

b = x;


So, I'm not aware that I ever use it in a string context. Are there any 
conditions though under which JavaScript might convert it to a string? 
It's used a fair bit as an array index.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [next] | [standalone]


#12550

From"Evertjan." <exjxw.hannivoort@interxnl.net>
Date2012-03-09 18:59 +0000
Message-ID<XnsA011CB5ABEB84eejj99@194.109.133.133>
In reply to#12524
Tim Streater wrote on 09 mrt 2012 in comp.lang.javascript:

> I got burnt today by doing:
> 
>   var a = b + 1;
> 
> Now b is supposed to be an integer, but for some reason at the time in 
> question, it was treated as a string.
> 
> The variable b is declared globally but not initialised there. During 
> its life it is initialised to the length of an array (as in 
> myarray.length). It's operated on as follows:
> 
> b--;
> x = b - 1;

why not make one line of the above two:

x = --b - 1;

or

x = b-- - 2;

> // some stuff done here

Watever b wass here is throw away by:

> b = x;

x could have been made a string in "some stuff",
like x = ''+x;
so b then is a string

> b = -1;

now b is of type float, there is no integer type in js
 
> b = 0;

now b is of type float, there is no integer type in js

 
> It may also be set to the index of a simple for-loop such as:
> 
> for (x=0; x<max; x++)
>    {
>    // Some code including break;
>    }
> 
> b = x;
> 
> So, I'm not aware that I ever use it in a string context. 

That is where debugging is for, insert here and there:

alert(typeof b); 

> Are there any 
> conditions though under which JavaScript might convert it to a string? 

yes, if you make it so:

b = '7';
b = ''+7;
b = 7.toString();
b = 7.concat();
b = myInput.value;
b = myElement.innerHTML;
etc.

> It's used a fair bit as an array index.

What is?





-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#12555

FromTim Streater <timstreater@greenbee.net>
Date2012-03-09 21:47 +0000
Message-ID<timstreater-BC2F41.21475709032012@news.individual.net>
In reply to#12550
In article <XnsA011CB5ABEB84eejj99@194.109.133.133>,
 "Evertjan." <exjxw.hannivoort@interxnl.net> wrote:

> Tim Streater wrote on 09 mrt 2012 in comp.lang.javascript:
> 
> > I got burnt today by doing:
> > 
> >   var a = b + 1;
> > 
> > Now b is supposed to be an integer, but for some reason at the time in 
> > question, it was treated as a string.
> > 
> > The variable b is declared globally but not initialised there. During 
> > its life it is initialised to the length of an array (as in 
> > myarray.length). It's operated on as follows:
> > 
> > b--;
> > x = b - 1;
> 
> why not make one line of the above two:

Because these are not sequential statements. I'm just listing the 
different operations I carry out on b at various places in my code. 
That's why I put blank lines between the different operations.

So in one place I do:

b--;

Elsewhere in the code I do:

x = b - 1;
// some stuff done here, doesn't reference b
b = x;

Elsewhere I do:

b = -1;

> > b = -1;
> 
> now b is of type float, there is no integer type in js

In the context of my post, does this matter?

Elsewhere I do:

b = 0;

and in a few places:

> > It may also be set to the index of a simple for-loop such as:
> > 
> > for (x=0; x<max; x++)
> >    {
> >    // Some code including break;
> >    }
> > 
> > b = x;

That's all the operations using operators that I carry out on b - apart 
from where I came unstuck with addition.

> > So, I'm not aware that I ever use it in a string context. 
> 
> That is where debugging is for, insert here and there:
> 
> alert(typeof b); 

I used console.info (), which immediately told me that b was '01'. Next 
time I "added" 1, it became '011' from which I conclude it had, somehow, 
become a string.

> 
> > Are there any 
> > conditions though under which JavaScript might convert it to a string? 
> 
> yes, if you make it so:
> 
> b = '7';
> b = ''+7;
> b = 7.toString();
> b = 7.concat();
> b = myInput.value;
> b = myElement.innerHTML;
> etc.

Well obviously. But according to my text editor, I do none of these. 
Before making my OP, I went through all my code to ensure I hadn't 
obviously overlooked something, and to find out what the operations were 
that I carry out on b. I then listed them in the post.

> > It's used a fair bit as an array index.
> 
> What is?

b is.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


#12580

FromRobG <rgqld@iinet.net.au>
Date2012-03-10 14:56 -0800
Message-ID<340a80e8-3328-4570-aee5-3e4465d0368b@kv10g2000pbc.googlegroups.com>
In reply to#12524
On Mar 9, 11:49 pm, Tim Streater <timstrea...@greenbee.net> wrote:
> I got burnt today by doing:
>
>   var a = b + 1;
>
> Now b is supposed to be an integer, but for some reason at the time in
> question, it was treated as a string.
>
> The variable b is declared globally but not initialised there. During
> its life it is initialised to the length of an array (as in
> myarray.length). It's operated on as follows:
>
> b--;
>
> x = b - 1;
> // some stuff done here
> b = x;

I think that is an obvious suspect. As Evertjan said, start looking
for where things go wrong and see were b gets converted to a string,
then try to isolate it as a simple test case.

Anyhow, wherever you assign a variable value to b, you could do:

  b = +x;

or more explicitly:

  b = Number(x);


--
Rob

[toc] | [prev] | [next] | [standalone]


#12582

FromTim Streater <timstreater@greenbee.net>
Date2012-03-10 23:14 +0000
Message-ID<timstreater-60ACED.23143110032012@news.individual.net>
In reply to#12580
In article 
<340a80e8-3328-4570-aee5-3e4465d0368b@kv10g2000pbc.googlegroups.com>,
 RobG <rgqld@iinet.net.au> wrote:

> On Mar 9, 11:49 pm, Tim Streater <timstrea...@greenbee.net> wrote:
> > I got burnt today by doing:
> >
> >   var a = b + 1;
> >
> > Now b is supposed to be an integer, but for some reason at the time in
> > question, it was treated as a string.
> >
> > The variable b is declared globally but not initialised there. During
> > its life it is initialised to the length of an array (as in
> > myarray.length). It's operated on as follows:
> >
> > b--;
> >
> > x = b - 1;
> > // some stuff done here
> > b = x;
> 
> I think that is an obvious suspect. As Evertjan said, start looking
> for where things go wrong and see were b gets converted to a string,
> then try to isolate it as a simple test case.
> 
> Anyhow, wherever you assign a variable value to b, you could do:
> 
>   b = +x;
> 
> or more explicitly:
> 
>   b = Number(x);

Mmmm. I will do some more research on this. The variable is not actually 
called b, so it's easy to grep for and search for in the source. But I 
can do some more testing to pin it down. For now I've used parseInt at 
the one place where I do  b+1.

I was hoping it would be something obvious I'd overlooked, that some 
kind soul would say "Ah you fool Streater, doing <whatever> is bound to 
turn it into a string!".

OK - thanks for pushing me towards more debugging. I quite enjoy that.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


#12588

FromDr J R Stockton <reply1210@merlyn.demon.co.uk>
Date2012-03-10 21:06 +0000
Message-ID<q7bVHgUMJ8WPFwQl@invalid.uk.co.demon.merlyn.invalid>
In reply to#12524
In comp.lang.javascript message <timstreater-677053.13490009032012@news.
individual.net>, Fri, 9 Mar 2012 13:49:00, Tim Streater
<timstreater@greenbee.net> posted:

>I got burnt today by doing:
>
> var a = b + 1;

You could alter the cooking with     var a = b - -1 // !

To help diagnosis, as b seems to be a general-purpose variable, you
could alter its identifier for different uses and see what happens.

-- 
 (c) John Stockton, nr London UK.  ?@merlyn.demon.co.uk  DOS 3.3 6.20 ; WinXP.
   Web  <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
   PAS EXE TXT ZIP via  <http://www.merlyn.demon.co.uk/programs/00index.htm>
   My DOS  <http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web