the better 'if'

Any person who programs in JavaScript is quite familiar with the 'if...else' statement and likely aware of the 'expression ? true : false' format. While the second format is smaller, there is another key advantage over the 'if' statement: it can be embedded into another statement. This can help reduce excessive 'if' statements when the logic contained within them is repetitive.

For example: if (apple==1){jack = 1 + 5;}else{jack=1 + 10} ...can be rewritten as... jack = 1 + (apple == 1 ? 5 : 10)
Published October 22, 2003 · Updated September 17, 2005
Categorized as JavaScript
Short URL: https://snook.ca/s/32