Skip to content

Boolean("false") parses to true #531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tuhlmann opened this issue Sep 30, 2015 · 4 comments
Closed

Boolean("false") parses to true #531

tuhlmann opened this issue Sep 30, 2015 · 4 comments

Comments

@tuhlmann
Copy link

In section "21.6 Booleans" you advice to use Boolean(x) to parse a value into a boolean.
Please note that parsing mostly any String results in true:

Boolean("false")
=> true

null, undefined and "" parse to false.

@ljharb
Copy link
Collaborator

ljharb commented Sep 30, 2015

That's not for parsing a value - that's for coercing a value. Parsing a string into a boolean is an undefined operation - you need to implement it yourself. If you want the truthy value that is the string "false" to coerce to false, you'd need to write a function that does that conversion for you.

@tuhlmann
Copy link
Author

Thanks for the clarification, after looking at the example again I see the difference.

@hshoff
Copy link
Member

hshoff commented Sep 30, 2015

Hi @tuhlmann, to follow up, just because I was typing this as this closed 😄

This is expected behavior as defined in #15.2

Strings evaluate to false if an empty string '', otherwise true

We use the Comparison Operators & Equality to outline the behavior of the ToBoolean abstract method.

🍻

@tuhlmann
Copy link
Author

tuhlmann commented Oct 1, 2015

@hshoff Thanks for the clarification.

For a seasoned Java developer Boolean(something) looks awefully familiar. In Java new Boolean(str) or the preferred static method Boolean.parseBoolean(str) do exactly this parsing stuff.

I guess that's where the confusion came from. Maybe a short note in this section would avoid this trap, saying this does not parse, but coerce...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants