From: FAQ server on 30 Jul 2010 19:00 ----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number? ----------------------------------------------------------------------- Variables are not typed; their values are. The conversion between a string and a number happens automatically. Since plus (`+`) is also used as in string concatenation, `'1' + 1` is equal to `'11'`. The string determines what `+` does. To overcome this, first convert the string to a number. For example: `+varname` or `Number(varname)` or `parseInt(varname, 10)` or `parseFloat(varname)`. Form control values are strings, as is the result from a `prompt` dialog. Convert these to numbers before performing addition by using the unary `+` operator: `+'1' + 1` result is `2`. Additional Notes: <URL: http://jibbering.com/faq/notes/type-conversion/> <URL: http://msdn.microsoft.com/en-us/library/67defydd%28VS.85%29.aspx> The complete comp.lang.javascript FAQ is at http://jibbering.com/faq/ -- The sendings of these daily posts are proficiently hosted by http://www.pair.com.
From: Dr J R Stockton on 1 Aug 2010 16:31 In comp.lang.javascript message <4c53597c$0$285$14726298(a)news.sunsite.dk >, Fri, 30 Jul 2010 23:00:02, FAQ server <javascript(a)dotinternet.be> posted: >FAQ Topic - Why does 1+1 equal 11? or How do I convert a >string to a number? >Form control values are strings, That is true even for checkboxes, it seems. In my IE & FF & Opera, checkbox.value is the string 'on'. In my Safari and Chrome, it is 'on' if checked, else ''. In all, I expect checkbox.checked is a boolean. -- (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 7. Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links. Command-prompt MiniTrue is useful for viewing/searching/altering files. Free, DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.
From: David Mark on 2 Aug 2010 03:36 On Aug 1, 4:31 pm, Dr J R Stockton <reply1...(a)merlyn.demon.co.uk> wrote: > In comp.lang.javascript message <4c53597c$0$285$14726...(a)news.sunsite.dk>, Fri, 30 Jul 2010 23:00:02, FAQ server <javascr...(a)dotinternet.be> > > posted: > > >FAQ Topic - Why does 1+1 equal 11? or How do I convert a > >string to a number? > >Form control values are strings, > > That is true even for checkboxes, it seems. Why wouldn't it be? > > In my IE & FF & Opera, checkbox.value is the string 'on'. Yes, that's the default value. > > In my Safari and Chrome, it is 'on' if checked, else ''. That's a bit of a wacky implementation, but shouldn't cause any trouble. Serialization functions skip checkboxes if they are unchecked, so the reported value is irrelevant. > > In all, I expect checkbox.checked is a boolean. It is.
From: Ry Nohryb on 2 Aug 2010 04:38 On Aug 1, 10:31 pm, Dr J R Stockton <reply1...(a)merlyn.demon.co.uk> wrote: > In comp.lang.javascript message <4c53597c$0$285$14726...(a)news.sunsite.dk>, Fri, 30 Jul 2010 23:00:02, FAQ server <javascr...(a)dotinternet.be> > > posted: > > >FAQ Topic - Why does 1+1 equal 11? or How do I convert a > >string to a number? > >Form control values are strings, > > That is true even for checkboxes, it seems. > > In my IE & FF & Opera, checkbox.value is the string 'on'. > > In my Safari and Chrome, it is 'on' if checked, else ''. > > In all, I expect checkbox.checked is a boolean. All html elements' attributes are always strings, ISTM. -- Jorge.
From: David Mark on 2 Aug 2010 04:49 On Aug 2, 4:38 am, Ry Nohryb <jo...(a)jorgechamorro.com> wrote: > On Aug 1, 10:31 pm, Dr J R Stockton <reply1...(a)merlyn.demon.co.uk> > wrote: > > > In comp.lang.javascript message <4c53597c$0$285$14726...(a)news.sunsite.dk>, Fri, 30 Jul 2010 23:00:02, FAQ server <javascr...(a)dotinternet.be> > > > posted: > > > >FAQ Topic - Why does 1+1 equal 11? or How do I convert a > > >string to a number? > > >Form control values are strings, > > > That is true even for checkboxes, it seems. > > > In my IE & FF & Opera, checkbox.value is the string 'on'. > > > In my Safari and Chrome, it is 'on' if checked, else ''. > > > In all, I expect checkbox.checked is a boolean. > > All html elements' attributes are always strings, ISTM. But that's not an attribute (it's a DOM property). Why does it seem the whole world is confused about this? The only (direct) way to get an attribute value is with the getAttribute method. It returns a string or null (the latter when the attribute is not present). IE's implementation of this method in versions prior to 8 (and Compatibility View in 8) is broken as designed (it returns property values). http://www.cinsoft.net/attributes.html
|
Next
|
Last
Pages: 1 2 Prev: can I make Javascript see binary data? Next: Anushka for Shape FX Hot Shot Thigh Gel |