javascript if statement question code example

Example 1: javascript question mark

//This is what is called a 'Conditional operator'
let result = condition ? value1 : value2
//               ^^        ^^      ^^
let result =  1 == 2   ?  "YES" : "NO!"
console.log(result)  //output: "NO!"

//Basically a short form of an if-else statment

Example 2: java question mark operator

//ternary operator
// takes in a conditional statement
// a is returned when the condition is true and vice versa
return (a == b) ? a : b;

int n = (a < b) ? a + 10: b;

Tags:

Java Example