Use if statement and Boolean conversion at the same time in JavaScript

Description

The following code shows how to use if statement and Boolean conversion at the same time.

Example


<!--   ww  w . j a  va 2s .com-->
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var aString = "Hello world!";
if (aString){
document.writeln("value is true"); //output
}
aString = "";
if (aString){
document.writeln("value is true");
}else{
document.writeln("value is false"); //output
}
aString = null;
if (aString){
document.writeln("value is true");
}else{
document.writeln("value is false"); //output
}
</script>
</head>
<body>

</body>
</html>

Click to view the demo

The code above generates the following result.

Use if statement and Boolean conversion at the same time in JavaScript