Upload
kelley-scott
View
218
Download
1
Embed Size (px)
Citation preview
Ternary Operators
© Copyright 2014, Fred McClurg All Rights Reserved
2
Ternary Operator: “if” Shortcut
Description:
Abbreviated notation of the “if” condition. Often used on one line. Used for returning a value upon a true or false condition.
Syntax:
condition ? trueValue : elseValue ;
3
Simple Ternary Operator
var word = "Alumn";var totalCount = 4;var maleCount = 1;
if ( maleCount >= 1 ) { // use masculine tense // append singular or plural ending word += ( totalCount == 1 ) ? "us" : "i";} else { // use feminine tense // append singular or plural ending word += ( totalCount == 1 ) ? "a" : "ae";}
var report = word + " attended: " + totalCount;console.log( report ); ternary.html