Converting a String to a Boolean Value in JavaScript: A Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why "true" is not equal to true?

\n\n

Imagine 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:

\n\n
let userPref = localStorage.getItem('notifications');\nif (userPref) {\n    // Enable notifications\n}\n
\n\n

And 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.

\n\n

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\n

Why can't you just use if()?

\n\n

Let'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.

\n\n

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
\n\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.

\n\n

Top 5 Ways to Convert a String to Boolean

\n\n

We tested all the popular methods from discussions on Stack Overflow and real projects. Here they are, from worst to best.

\n\n

Method 1: Ternary Operator (most obvious, but verbose)

\n\n
function 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\n

Pros: Clear for beginners.
\nCons: Code grows if you need to handle "yes", "1", "on". You'll have to write a chain of conditions.

\n\n

Method 2: JSON.parse() (elegant, but strict)

\n\n
function 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\n

The 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.

\n\n

Method 3: Regular Expression (flexible, but slow)

\n\n
function 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\n

Here we use the i flag (case-insensitivity) and handle several "truthy" variants: true, yes, 1,

Blogs

Book Recommendations