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


Groups > comp.lang.javascript > #12555

Re: Integer vs. string

From Tim Streater <timstreater@greenbee.net>
Newsgroups comp.lang.javascript
Subject Re: Integer vs. string
Date 2012-03-09 21:47 +0000
Organization Personal Software
Message-ID <timstreater-BC2F41.21475709032012@news.individual.net> (permalink)
References <timstreater-677053.13490009032012@news.individual.net> <XnsA011CB5ABEB84eejj99@194.109.133.133>

Show all headers | View raw


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

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web