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


Groups > comp.lang.javascript > #12550

Re: Integer vs. string

Newsgroups comp.lang.javascript
Subject Re: Integer vs. string
From "Evertjan." <exjxw.hannivoort@interxnl.net>
References <timstreater-677053.13490009032012@news.individual.net>
Message-ID <XnsA011CB5ABEB84eejj99@194.109.133.133> (permalink)
Date 2012-03-09 18:59 +0000

Show all headers | View raw


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)

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