shorthand syntax javascript code example

Example 1: javascript if shorthand

condition ? doThisIfTrue : doThisIfFalse

1 > 2 ? console.log(true) : console.log(false)
// returns false

Example 2: ternary operator shorthand javascript

let startingNum = startingNum ? otherNum : 1
// can be expressed as
let startingNum = otherNum || 1

// Another scenario not covered here is if you want the value 
// to return false when not matched. 
//The JavaScript shorthandfor this is:
let startingNum = startingNum ? otherNum : 0
// But it can be expressed as
let startingNum = startingNum && otherNum

Example 3: if shorthand typescript

const answer = x > 10 ? "greater than 10" : "less than 10";

Tags:

Php Example