Skip to content

JavaScript if or statement | Example code

  • by

Use the logical OR ( || ) operator in the if statement, if one or both of the conditions are true, then the code inside the if statement will execute in JavaScript.

if (A || B){
   // code

JavaScript if or

Simple example code isSale variable is set to false, the code inside the if block will still execute because the credit variable is set to true.

<!DOCTYPE html>

    const credit = true;
    const isSale = false;

    if (credit || isSale) {
      console.log("Can do shopping.");
    } else {
      console.log("Can not do shopping.");



JavaScript if or statement

How to use OR condition in a JavaScript IF statement?

Answer: More than one condition statement is needed to use OR(||) operator in if conditions and notation is ||.

if(condition || condition){ 
   some stuff

If you have multiple conditions that you want to check against, you can use logical operators like && (AND), || (OR), and ! (NOT) to combine conditions within the if statement.

For example:

let hour = 12;
let greeting;

if (hour < 12) {
    greeting = "Good morning";
} else if (hour >= 12 && hour < 18) {
    greeting = "Good afternoon";
} else {
    greeting = "Good evening";


In this case:

  • If hour is less than 12, it prints “Good morning”.
  • If hour is greater than or equal to 12 and less than 18, it prints “Good afternoon”.
  • Otherwise, it prints “Good evening”.

Do comment if you have any doubts or suggestions on this JS if statement with OR operator code.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *