0
0
JavascriptHow-ToBeginner · 2 min read

JavaScript How to Convert String to Boolean Easily

To convert a string to boolean in JavaScript, compare the string to a truthy value like const bool = (str === 'true') which returns true if the string is exactly 'true', otherwise false.
📋

Examples

Input'true'
Outputtrue
Input'false'
Outputfalse
Input'hello'
Outputfalse
🧠

How to Think About It

To convert a string to a boolean, think about what strings should mean true or false. Usually, the string 'true' means true and anything else means false. So you check if the string equals 'true' exactly, then return true; otherwise, return false.
📐

Algorithm

1
Get the input string.
2
Check if the string is exactly equal to 'true'.
3
If yes, return true.
4
Otherwise, return false.
💻

Code

javascript
const strToBool = (str) => str === 'true';

console.log(strToBool('true'));  // true
console.log(strToBool('false')); // false
console.log(strToBool('hello')); // false
Output
true false false
🔍

Dry Run

Let's trace the input 'true' through the code

1

Input string

str = 'true'

2

Compare string

'true' === 'true' is true

3

Return result

Returns true

Input StringComparison ResultReturned Boolean
'true'truetrue
'false'falsefalse
'hello'falsefalse
💡

Why This Works

Step 1: Exact match check

Using === checks if the string is exactly 'true', so only that string returns true.

Step 2: Boolean result

The comparison returns a boolean value directly, so no extra conversion is needed.

Step 3: Simple and clear

This method is simple and avoids confusion from other string values.

🔄

Alternative Approaches

Using JSON.parse
javascript
const strToBool = (str) => {
  try {
    return JSON.parse(str.toLowerCase());
  } catch {
    return false;
  }
};

console.log(strToBool('true'));  // true
console.log(strToBool('false')); // false
console.log(strToBool('hello')); // false
This parses 'true' and 'false' strings but throws error for others, so we catch and return false.
Using a set of truthy strings
javascript
const truthyStrings = new Set(['true', 'yes', '1']);
const strToBool = (str) => truthyStrings.has(str.toLowerCase());

console.log(strToBool('true'));  // true
console.log(strToBool('yes'));   // true
console.log(strToBool('no'));    // false
Allows more flexible true values but requires defining which strings count as true.

Complexity: O(1) time, O(1) space

Time Complexity

The comparison str === 'true' runs in constant time because it only checks fixed-length strings.

Space Complexity

No extra memory is used besides the input string and a boolean return value, so space is constant.

Which Approach is Fastest?

The direct equality check is fastest and simplest. JSON.parse adds overhead and error handling. Using a set is flexible but uses more memory.

ApproachTimeSpaceBest For
Direct equality (str === 'true')O(1)O(1)Simple exact match
JSON.parse with try/catchO(1)O(1)Parsing JSON boolean strings
Set of truthy stringsO(1)O(n) for set sizeFlexible true values
💡
Use strict equality str === 'true' for clear and simple string to boolean conversion.
⚠️
Trying to convert strings directly with Boolean(str) returns true for any non-empty string, not the boolean meaning of the string content.