Introduction: Why "true" is not equal to true?
\n\nImagine a scenario: you are writing a frontend for an online store. A user selects the option "Receive email notifications", and your code saves it in localStorage as the string "true". On the next visit, you read this string and try to check the condition:
let userPref = localStorage.getItem('notifications');\nif (userPref) {\n // Enable notifications\n}\n\n\nAnd here's the surprise — the condition always triggers! Even if the string "false" is stored in localStorage, JavaScript considers it a truthy value. Not because you did something wrong, but because any non-empty string in JS is true.
This problem is so popular that the question on Stack Overflow has gathered thousands of views and dozens of solution variants. Today, we will once and for all figure out how to correctly convert a string to a boolean in JavaScript, look at 5 working methods, and identify the best one.
\n\nWhy can't you just use if()?
\n\nLet's address this question right away. Many beginners think: "If I write if(str), the string "false" will turn into false." This is a dangerous misconception.
In JavaScript, there is the concept of falsy values. There are only 6 of them:
\n- \n
false\n 0(zero) \n ""(empty string) \n null\n undefined\n NaN\n
Everything else is truthy. The string "false" is not empty; it has 5 characters. Therefore, it is truthy. The same goes for "0", "null", and even "undefined". Simple coercion via if() or Boolean() will not work for strings.
Top 5 Ways to Convert a String to Boolean
\n\nWe tested all the popular methods from discussions on Stack Overflow and real projects. Here they are, from worst to best.
\n\nMethod 1: Ternary Operator (most obvious, but verbose)
\n\nfunction stringToBoolean(str) {\n return str === 'true' ? true : str === 'false' ? false : null;\n}\n\nconsole.log(stringToBoolean('true')); // true\nconsole.log(stringToBoolean('false')); // false\nconsole.log(stringToBoolean('yes')); // null\n\n\nPros: Clear for beginners.
\nCons: Code grows if you need to handle "yes", "1", "on". You'll have to write a chain of conditions.
Method 2: JSON.parse() (elegant, but strict)
\n\nfunction stringToBoolean(str) {\n try {\n return JSON.parse(str);\n } catch (e) {\n return null;\n }\n}\n\nconsole.log(stringToBoolean('true')); // true\nconsole.log(stringToBoolean('false')); // false\nconsole.log(stringToBoolean('TRUE')); // Error! Returns null\n\n\nThe JSON.parse() method can parse the strings "true" and "false" into boolean values. But there is a nuance: it is case-sensitive. "True" or "TRUE" will cause an error. However, JSON.parse("null") will return null, not false — this can be useful.
Method 3: Regular Expression (flexible, but slow)
\n\nfunction stringToBoolean(str) {\n if (typeof str !== 'string') return null;\n return /^(true|yes|1|on)$/i.test(str.trim());\n}\n\nconsole.log(stringToBoolean('true')); // true\nconsole.log(stringToBoolean('YES')); // true\nconsole.log(stringToBoolean('off')); // false\nconsole.log(stringToBoolean('0')); // false\n\n\nHere we use the i flag (case-insensitivity) and handle several "truthy" variants: true, yes, 1,