Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number? (2010-07-31)

1 view
Skip to first unread message

FAQ server

unread,
Jul 30, 2010, 7:00:02 PM7/30/10
to
-----------------------------------------------------------------------
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.

Dr J R Stockton

unread,
Aug 1, 2010, 4:31:07 PM8/1/10
to
In comp.lang.javascript message <4c53597c$0$285$1472...@news.sunsite.dk
>, Fri, 30 Jul 2010 23:00:02, FAQ server <javas...@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>.

David Mark

unread,
Aug 2, 2010, 3:36:45 AM8/2/10
to
On Aug 1, 4:31 pm, Dr J R Stockton <reply1...@merlyn.demon.co.uk>
wrote:
> In comp.lang.javascript message <4c53597c$0$285$14726...@news.sunsite.dk>, Fri, 30 Jul 2010 23:00:02, FAQ server <javascr...@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.

Ry Nohryb

unread,
Aug 2, 2010, 4:38:12 AM8/2/10
to
On Aug 1, 10:31 pm, Dr J R Stockton <reply1...@merlyn.demon.co.uk>
wrote:
> In comp.lang.javascript message <4c53597c$0$285$14726...@news.sunsite.dk>, Fri, 30 Jul 2010 23:00:02, FAQ server <javascr...@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.

David Mark

unread,
Aug 2, 2010, 4:49:28 AM8/2/10
to

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

Ry Nohryb

unread,
Aug 2, 2010, 11:34:00 AM8/2/10
to
On Aug 2, 10:49 am, David Mark <dmark.cins...@gmail.com> wrote:
>
> But that's not an attribute (it's a DOM property).  Why does it seem
> the whole world is confused about this?

I said "All *html*elements'*attributes* are always strings, ISTM.
--
Jorge.

Ry Nohryb

unread,
Aug 2, 2010, 11:41:32 AM8/2/10
to
On Aug 2, 10:49 am, David Mark <dmark.cins...@gmail.com> wrote:
> (...)

> The only (direct) way to get an attribute value is with the
> getAttribute method. (...)

That's not true. Many/most/all? -standard- attributes are (directly)
reflected in JSLand as properties, e.g. document.body.style.marginTop
or document.body.bgColor. It might not be so in the most borken
browser ever... (?)
--
Jorge.

David Mark

unread,
Aug 2, 2010, 11:42:57 AM8/2/10
to

Go back and read the part you snipped and you will see why I corrected
your mistake. It doesn't matter if you knew what you were saying.
The context in which you said it was wrong, making it look like there
was confusion about the issue of the checked property of a checkbox.

And attributes aren't anything. They are an abstract concept. You
can retrieve attribute values as I indicated and, if present, they
will be returned in string form (in most browsers).

RobG

unread,
Aug 2, 2010, 9:32:00 PM8/2/10
to
On Aug 2, 6:31 am, Dr J R Stockton <reply1...@merlyn.demon.co.uk>
wrote:
> In comp.lang.javascript message <4c53597c$0$285$14726...@news.sunsite.dk>, Fri, 30 Jul 2010 23:00:02, FAQ server <javascr...@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 ''.

It should return whatever string value you have assigned to the
element's value attribute. The value attribute is required in HTML
4.01 for radio buttons and checkboxes.

<URL: http://www.w3.org/TR/html401/interact/forms.html#adef-value-INPUT
>

I'll take a punt and say you didn't assign a value and are getting
whatever the browser decides to assign to invalid markup. Play with
the following:

<div>
<input type="radio" name="rad0" onclick="
alert(
'value property: ' + this.value +
'\nvalue attribute: ' + this.getAttribute('value') +
'\ntypeof checked property: ' +
(typeof this.checked) + ' ' + this.checked +
'\ntypeof checked attribute: ' +
(typeof this.getAttribute('checked')) + ' ' +
this.getAttribute('checked')
);
">
</div>

HTML 5 is vague about it:

"Form controls have a value and a checkedness. (The latter is only
used by input elements.) These are used to describe how the user
interacts with the control."

<URL: http://www.w3.org/TR/html5/association-of-controls-and-forms.html#concept-fe-value
>

The word "checkedness" is awkward and confusing, what's wrong with
"checkedstate"? I don't think value has anything to do with how a user
interacts with a control.


> In all, I expect checkbox.checked is a boolean.

The checked property is specified in DOM 2 HTML as being boolean. Any
other type is non-conforming.

<URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-30233917 >


--
Rob

Ry Nohryb

unread,
Aug 3, 2010, 5:35:18 AM8/3/10
to
On Aug 3, 3:32 am, RobG <rg...@iinet.net.au> wrote:
> On Aug 2, 6:31 am, Dr J R Stockton <reply1...@merlyn.demon.co.uk>
> wrote:
> (...)

>
> > In all, I expect checkbox.checked is a boolean.
>
> The checked property is specified in DOM 2 HTML as being boolean. Any
> other type is non-conforming. (...)

The type of the checked *attribute* is boolean, but its value as
reflected in JSLand is *not* of boolean type.
--
Jorge.

0 new messages