Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Convert String to Boolean</title> </head> <body> <script> // Defining a custom function function stringToBoolean(value){ return (String(value) === '1' || String(value).toLowerCase() === 'true'); } // Performing some tests console.log(stringToBoolean(true)); // Prints: true console.log(stringToBoolean("true")); // Prints: true console.log(stringToBoolean("True")); // Prints: true console.log(stringToBoolean("TRUE")); // Prints: true console.log(stringToBoolean(false)); // Prints: false console.log(stringToBoolean("false")); // Prints: false console.log(stringToBoolean("False")); // Prints: false console.log(stringToBoolean(undefined)); // Prints: false console.log(stringToBoolean(null)); // Prints: false console.log(stringToBoolean(1)); // Prints: true console.log(stringToBoolean(0)); // Prints: false </script> <p><strong>Note:</strong> Press Ctrl+Shift+J (Windows / Linux) or Cmd+Opt+J (Mac) to open Browser Console panel.</p> </body> </html>