Expressions in JavaScript Ternary Operator and JSLint Expressions in JavaScript Ternary Operator and JSLint javascript javascript

Expressions in JavaScript Ternary Operator and JSLint


It's an expression. It's equivalent to writing

0 === 1;

You're writing an expression that has immediate side effects and that's considered bad.

Generally expressions are useless statements that have no side effect. It's considered better form to simply do

if (s === "test") {  MyFunc();} else {  MyFunc2();}

Apart from that it's perfectly solid syntax. I personally do agree that writing a terse ternary as an alternative to an if is bad and you're better off only using it for assignment.

Other short hand expression that have been (ab)used for terse-ness

someCondition && doMagic(magic);someCondition || doMagic(magic);

Again these are considered bad form if there used only as expressions because using these just obscures logic away and make it harder to maintain code.

JSHint has an option expr for this. See ticket

Running:

/*jshint  expr: true*/var s, MyFunc, MyFunc2;s === "test" ? MyFunc() : MyFunc2();0 === 1;

Will pass