How to useJavaScript == Operator in Javascript

This operator compares the equality of two operands. If equal then the condition is true otherwise false. 

Example: This example uses the == operator to convert a string to its boolean value. 

Javascript
let str1 = "true";
console.log(str1 == 'true');

let str2 = "True";
console.log(str2.toLowerCase() == 'true');

Output
true
true

JavaScript Convert a string to boolean

To Convert a string to Boolean, we have multiple approaches. We are going to learn how to Convert a string to Boolean.

Below are the approaches used to Convert a string to Boolean:

Table of Content

  • Approach 1: Using JavaScript == Operator
  • Approach 2: Using JavaScript === Operator
  • Approach 3: Using the Boolean() function
  • Approach 4: Using a regular expression
  • Approach 5: Using the !! (double negation) operator
  • Approach 6: Using JSON.parse()
  • Approach 7: Using Ternary Operator
  • Approach 8: Using Switch Case
  • Approach 9: Using a Custom Map Function

Similar Reads

Approach 1: Using JavaScript == Operator

This operator compares the equality of two operands. If equal then the condition is true otherwise false....

Approach 2: Using JavaScript === Operator

This operator compares the equality of two operands with type. If equal(type and value both) then the condition is true otherwise false....

Approach 3: Using the Boolean() function

The boolean function returns the boolean value of the variable. It can also be used to find the boolean result of a condition, expression, etc. The Boolean() function evaluates the input and returns a boolean value. In this case, since the input string “true” is non-empty, the Boolean() function returns true....

Approach 4: Using a regular expression

In this approach regular expression to convert a string to a boolean value in JavaScript....

Approach 5: Using the !! (double negation) operator

The !! (double negation) operator is a commonly used approach to convert a string to a boolean value in JavaScript...

Approach 6: Using JSON.parse()

The JSON.parse() method can be used to convert a string to a boolean value in JavaScript. JSON.parse() method is used to parse the string “true” and convert it into its corresponding boolean value, which is true....

Approach 7: Using Ternary Operator

When using the ternary operator to convert a string to a boolean, the code employs a concise one-liner. The toLowerCase() method is applied to ensure case-insensitive comparison. The ternary operator checks if the lowercase string is equal to ‘true’. If the condition is met, it returns true; otherwise, it returns false....

Approach 8: Using Switch Case

The switch case approach involves using a switch statement to evaluate the lowercase string. Cases are defined for ‘true’ and ‘false’, each returning the corresponding boolean value. The default case is included to handle invalid input by throwing an error....

Approach 9: Using a Custom Map Function

This approach uses a custom map to handle specific boolean-like strings, providing a flexible way to convert a string to a boolean....