How to convert a string to a boolean in JavaScript

#javascript #string

Last updated on Sep 27, 2020 by Suraj Sharma



In this tutorial, you will learn to convert a string to a boolean in JavaScript


Consider, a user has entered a 'true' or a 'false' value in the input field in the form.


To convert the 'true' or 'false' string value to a boolean we can do like this


const value = 'true';

const booleanValue = value === 'true'; // true


To handle case-sensitive strings.


const value = 'False';

const booleanValue = value.toLowerCase() === 'true'; // false


Related Articles



Suraj Sharma is a JavaScript Software Engineer. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.