JavaScript

StringtoBool Method

Syntax

String.toBool([explicit])

Arguments

explicitboolean

Whether or not to force falsy values to be matched.

Returns

valueboolean

The value of the string as a boolean.

Description

Extension to the native string variable to convert a string into a boolean.

Discussion

The String.toBool method will use any of the values stores in A5.d.bool.truthy and A5.d.bool.falsy arrays as true and false values.

If the "explicit" argument is set to true then if the value of the string is not found in the A5.d.bool.truthy or A5.d.bool.falsy arrays, a null will be returned. Otherwise a false will be returned whenever the value is not found in the A5.d.bool.truthy array.

Example

var str1 = 'foo';
var str2 = 'yes';
var str3 = 'no';
var b = str1.toBool();
// b = false
b = str1.toBool(true);
// b = null
b = str2.toBool(true);
// b = true
b = str3.toBool(true);
// b = false