Convert a nonempty string to Boolean type in JavaScript

Description

The following code shows how to convert a nonempty string to Boolean type.

Example


<!--  ww w  . ja  va 2s  . c o m-->

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var aString = "Hello world!";
var aBoolean = Boolean(aString);
document.writeln(aBoolean);
</script>
</head>
<body>

</body>
</html>

Click to view the demo

The code above generates the following result.

Convert a nonempty string to Boolean type in JavaScript