Ternary Operator in JavaScript Language

Ternary Operator

On this page

Ternary Operator

Program to use Ternary Operator JavaScript Language

let a = 10;
    let b = 20;
    let max;
    max = a > b ? a : b;
    console.log("Max Number is =", max);
    

Output

Max Number is = 20
    

Steps Description

  • Declare three variables a, b and max
  • Initialize three variables with value of 10, 20 respectively a=10, b=20 and max
  • Ternary operator works like if else
  • condition is checked first value is true expression1 displayed condition is false expression2 displayed
  • console.log inbuild function to display max variable number