Last active
June 20, 2016 10:33
-
-
Save simov/c26335c4e881bbab72a6284399bc759f to your computer and use it in GitHub Desktop.
Conditional (ternary) Operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var func = function (arg) { | |
if (arg.yes) { | |
return 'yes' | |
} | |
else if (arg.no) { | |
return 'no' | |
} | |
else if (arg.probably) { | |
return 'probably' | |
} | |
else { | |
return null | |
} | |
} | |
console.log(func({yes: true})) | |
console.log(func({no: true})) | |
console.log(func({probably: true})) | |
console.log(func({})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var func = function (arg) { | |
return true && | |
(arg.yes && | |
'yes' | |
|| (arg.no && | |
'no' | |
|| (arg.probably && | |
'probably' | |
|| null))) | |
} | |
console.log(func({yes: true})) | |
console.log(func({no: true})) | |
console.log(func({probably: true})) | |
console.log(func({})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var func = function (arg) { | |
return true && | |
(arg.yes ? | |
'yes' | |
: (arg.no ? | |
'no' | |
: (arg.probably ? | |
'probably' | |
: null))) | |
} | |
console.log(func({yes: true})) | |
console.log(func({no: true})) | |
console.log(func({probably: true})) | |
console.log(func({})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment