js convert string to boolean
JavaScript[Edit]
+
0
-
0
js convert string to boolean
1 2 3 4var text = 'true'; var value = (text == 'true'); console.log(value); // true
[Edit]
+
0
-
0
js convert string to boolean
1 2 3 4 5 6 7 8 9 10 11 12 13function parseBoolean(text) { var value = JSON.parse(text); if(typeof value != 'boolean') throw new Error('Incorrect type!'); return value; } var text = 'true'; var value = parseBoolean(text); console.log(value);
[Edit]
+
0
-
0
js convert string to boolean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17var truthExpression = /^(TRUE|True|true|YES|Yes|yes|ON|On|on|1|T|t|Y|y)$/g; var falsyExpression = /^(FALSE|False|false|NO|No|no|OFF|Off|off|0|F|f|N|n)$/g; function parseBoolean(text) { if (truthExpression.test(text)) return true; if (falsyExpression.test(text)) return false; throw new Error('Incorrect value!'); } var text = 'true'; var value = parseBoolean(text); console.log(value);
[Edit]
+
0
-
0
js convert string to boolean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47function parseBoolean(value) { switch(value) { case 1: case true: case "1": case "true": case "True": case "TRUE": case "on": case "On": case "ON": case "yes": case "Yes": case "YES": case "t": case "T": case "y": case "Y": return true; case 0: case false: case "0": case "false": case "False": case "FALSE": case "off": case "Off": case "OFF": case "no": case "No": case "NO": case "f": case "F": case "n": case "N": return false; default: throw new Error('Incorrect value!'); } } var text = 'true'; var value = parseBoolean(text); console.log(value); // true
[Edit]
+
0
-
0
js convert string to boolean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17var truthExpression = /^(TRUE|True|true|YES|Yes|yes|ON|On|on|1|T|t|Y|y)$/g; var falsyExpression = /^(FALSE|False|false|NO|No|no|OFF|Off|off|0|F|f|N|n)$/g; function parseBoolean(text) { if (truthExpression.test(text)) return true; if (falsyExpression.test(text)) return false; throw new Error('Incorrect value!'); } var text = 'true'; var value = parseBoolean(text); console.log(value); // true
[Edit]
+
0
-
0
js convert string to boolean
1 2 3 4 5 6 7 8 9 10 11 12 13function parseBoolean(text) { var value = JSON.parse(text); if(typeof value != 'boolean') throw new Error('Incorrect type!'); return value; } var text = 'true'; var value = parseBoolean(text); console.log(value); // true
[Edit]
+
0
-
0
js convert string to boolean
1 2 3 4var text = 'true'; var value = (text == 'true'); console.log(value); // true