question mark operator js 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: single if statement js true false

condition ? exprIfTrue : exprIfFalse

Example 3: examples of Conditional Operator js

var age = 19;
var canDrive = age > 16 ? 'yes' : 'no';

Example 4: question mark operator javascript

voteable = (age < 18) ? "Too young":"Old enough"
//         condition  ?   if true  :  if false

Tags:

Misc Example