Parses the string argument as a boolean
. The boolean
returned represents the value true
if the string argument is not null
and is equal, ignoring case, to the string "true"
.
When the Boolean.parse function is called with a string argument the following steps are taken:
- If Type(string) is not
"string"
- Return
false
- Return
- Let normalizedString be the result of applying
String.prototype.toLowerCase
on the string - If normalizedString is
"true"
- Return
true
- Return
- Return
false
Boolean.parse('true'); // true
Boolean.parse('false'); // false
// In contrast with problematic:
Boolean('false'); // true
Storages which doesn't support actual boolean values at serialization, and only have string-based value types. Example: Node's env
arguments.
Boolean.parseBoolean seems rather redundant as a name. Why isn't it just called Boolean.parse? The "Int" in Number.parseInt is a qualifier that is needed because what is recognized is only a subset of string encoded Numbers. But this operation deals with all the normal string values produced by toStringing a Boolean value. So the qualification is not really needed.
Normally, operations like this would do a ToString on the argument value and then process that rather than what you do in step 1.
It's weird that you return
false
for anything that isn't "true". For example, a typo like "trye" returnfalse
. I think you should treat anything that isn't "true"/"false" (although you probably should trim leading/trailing blanks) as an throwing error.