20 JavaScript Questions on Conditional Statements & Ternary Operator

Last Updated: June 16, 2025

Understanding conditional statements and logical operators is fundamental to mastering JavaScript programming. Whether you’re validating user input, creating dynamic UI interactions, or implementing complex business logic, the ability to make decisions in code is essential. JavaScript provides several powerful tools for this purpose: if, else if, and else statements allow for structured control flows; logical operators like && (AND), || (OR), and ! (NOT) enable compound conditions; and the ternary operator offers a shorthand way to return values based on a condition. This set of 20 thoughtfully designed questions will help reinforce your understanding of these concepts, improve your logical thinking, and prepare you for real-world coding scenarios or technical interviews. Whether you’re a beginner or brushing up your skills, these exercises are a great way to put theory into practice.

Here are 20 JavaScript questions using conditional statements, logical operators (&&, ||, !), and the ternary operator (? :). These questions are beginner-to-intermediate level and suitable for quizzes, interviews, or practice.

🔹 Conditional Statements (if, if-else, else-if)

  1. Check if a number is positive.
    let num = 10;
    if (/* your condition here */) {
      console.log("Positive number");
    }
    
  2. Check if a number is even or odd.
    let num = 7;
    if (/* your condition */) {
      console.log("Even");
    } else {
      console.log("Odd");
    }
    
  3. Check if a number is divisible by both 3 and 5.
    let num = 15;
    if (/* your condition */) {
      console.log("Divisible by 3 and 5");
    }
    
  4. Check the grade using marks.
    let marks = 75;
    if (marks >= 90) {
      console.log("A");
    } else if (marks >= 75) {
      console.log("B");
    } else {
      console.log("C");
    }
    
  5. Check if a year is a leap year.
    let year = 2024;
    if (/* leap year condition */) {
      console.log("Leap Year");
    } else {
      console.log("Not a Leap Year");
    }
    
  6. Check if a number is in range 10 to 50 (inclusive).
    let num = 30;
    if (/* condition */) {
      console.log("In range");
    } else {
      console.log("Out of range");
    }
    
  7. Check if the input is a number.
    let input = "123";
    if (/* use typeof or isNaN */) {
      console.log("Valid number");
    } else {
      console.log("Not a number");
    }
    
  8. Check if a user is logged in and is an admin.
    let isLoggedIn = true;
    let isAdmin = false;
    if (/* condition with && */) {
      console.log("Access granted");
    } else {
      console.log("Access denied");
    }
    

🔹 Logical Operators (&&, ||, !)

  1. Check if age is between 18 and 60.
    let age = 25;
    if (age >= 18 && age <= 60) {
      console.log("Allowed");
    } else {
      console.log("Not Allowed");
    }
    
  2. Check if a user has either email or phone verified.
let emailVerified = false;
let phoneVerified = true;
if (/* condition with || */) {
  console.log("User verified");
}
  1. Check if a number is not zero.
let num = 5;
if (!num == 0) {
  console.log("Non-zero number");
}
  1. Check if username is not empty and password is strong.
let username = "john";
let password = "john1234!";
if (username && password.length > 7) {
  console.log("Valid Credentials");
}
  1. Check if either of the two variables is a string.
let a = "hello";
let b = 123;
if (typeof a === "string" || typeof b === "string") {
  console.log("At least one is a string");
}
  1. Check if a user has neither admin nor editor role.
let role = "viewer";
if (!(role === "admin" || role === "editor")) {
  console.log("No access");
}

🔹 Ternary Operator (? 🙂

  1. Check if a number is even using ternary.
let num = 4;
let result = (num % 2 === 0) ? "Even" : "Odd";
console.log(result);
  1. Display login status.
let isLoggedIn = false;
console.log(isLoggedIn ? "Welcome!" : "Please login");
  1. Assign grade based on score using nested ternary.
let score = 85;
let grade = score >= 90 ? "A" : score >= 75 ? "B" : "C";
console.log(grade);
  1. Display if a number is positive, negative or zero using ternary.
let num = 0;
let result = num > 0 ? "Positive" : num < 0 ? "Negative" : "Zero";
console.log(result);
  1. Check if user has access based on role.
let role = "admin";
let access = (role === "admin" || role === "editor") ? "Granted" : "Denied";
console.log(access);
  1. Toggle status message.
let isOnline = true;
let status = isOnline ? "Online" : "Offline";
console.log(status);