Your example is indeed valid javascript. You can use a comma to separate expressions, and wrap that in a single statement with parentheses for the ternary.
var functionOne = function() { console.log(1); }
var functionTwo = function() { console.log(2); }
var functionThree = function() { console.log(3); }
var isTrue = true;
isTrue ? (functionOne(), functionTwo()) : functionThree();
// 1
// 2
isTrue = false;
isTrue ? (functionOne(), functionTwo()) : functionThree();
// 3
However, this is not advisable. Your version with an if
statement is far more clear and readable, and will execute just as fast. In most codebases I've ever seen or worked with, the comma operator is never used this way as it's far more confusing than it is helpful.
Just because you can, doesn't mean you should.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…