question mark in javascript code example

Example 1: js ternary

condition ? ifTrue : ifFalse

Example 2: 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 3: question mark and colon in javascript

const isBlack = false;const text = isBlack ? 'Yes, black!' : 'No, something else.';console.log(text);// "No, something else."

Example 4: single if statement js true false

condition ? exprIfTrue : exprIfFalse

Example 5: javascript conditional ? :

//XCal - Javascript Inline Conditional Sample (condition brackets only for clarity)
//                           v-? = Conditional Operator
//                           v                  v-: = True/False result value separator
//Format =    v-Condition-v  ?  v-When True-v   :  v-When False-v
var vResult = (null == null) ? 'Condition True' : 'Condition False';
//vResult is now 'Condition True';

Example 6: ternary operator in javascript

FullName: (obj.FirstName && obj.LastName) ? obj.FirstName + " " + obj.LastName : "missing values",

Tags:

Misc Example