10 biggest JS pitfalls

Claude Pache claude.pache at gmail.com
Mon Dec 31 10:32:32 PST 2012


Le 31 déc. 2012 à 18:58, Axel Rauschmayer <axel at rauschma.de> a écrit :

>> Personally, I have issues with from- and to-String conversions when working with HTML form field values and data-* attributes. More precisely, I am careful to make these conversions manually, and I regret that JS doesn't warn me if I forget to do them. Indeed the following cases are problematic:
>> 
>> * String-to-Number : not triggered in, e.g.,  x + '1' when x is a Number.
> 
> I would assume the opposite being a problem: The user enters the number '33' in a form field, you try to add 1, but forget that it is a string and end up with '331' instead of 34.
It is what I meant: (but I should have written: x + myFormField.value to be clearer).

> 
>> * Boolean-to-String, Null-to-String: false and null are not converted to a falsy string.
> 
> When does this happen? Can you explain?

When you store «false» in a HTML data-* attribute, and retrieve it later from there, you don't get a falsy value if you had naively relied on the implicit conversions. Or, when you put a boolean in a hidden form field and expect it to be treated as truthy/falsy server-side by the program receiving the form.

—Claude


More information about the es-discuss mailing list