Conditional (Ternary) Operator vs if statement

In this example below you will see how to do a Conditional (Ternary) Operator vs if statement with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by shervinkayvon, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright shervinkayvon ©
  • HTML
  • CSS
  • JavaScript
    

/*Downloaded from https://www.codeseek.co/shervinkayvon/conditional-ternary-operator-vs-if-statement-xPKLOB */
    


/*Downloaded from https://www.codeseek.co/shervinkayvon/conditional-ternary-operator-vs-if-statement-xPKLOB */
    /* Should be used to return a value. 
Ternary stands for three so typically it should be variable, condition, and statement that is executed.
*/

let num1 = 10
let num2 = 20

// ternary
let results = num1 === 10 && num2 === 20 ? 'its BOTH (Ternary)' :  'its NOT BOTH (Ternary)' 
console.log(results)

// if statement 
if (num1 === 10 && num2 === 20) {
	console.log('its BOTH (if statement)')	
} else {
	console.log('its NOT BOTH (if statement)')
}

Comments