JavaScript if not used with non-Boolean values, it returns false if its single operand can be converted to true.
Use the Logical NOT !
operator:
if(!condition){
expression();
}
JavaScript if not an example
simple example code.
<!DOCTYPE html>
<html>
<body>
<script>
var isDay = false;
if (!isDay) {
console.log("It's night");
}
</script>
</body>
</html>
Output:
![JavaScript if not](https://i0.wp.com/tutorial.eyehunts.com/wp-content/uploads/2022/03/JavaScript-if-not.jpg?resize=272%2C106&ssl=1)
How to check if a variable is not null?
Answer: Here is how you can test if a variable is not NULL:
if (myVar !== null) {...}
the block will be executed if myVar is not null.. it will be executed if myVar is undefined or false or 0
or NaN
or anything else.
Read more: JavaScript isNull
Do comment if you have any doubts or suggestions on this JS if topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version