Shorthand for testing more values of same property in if statement?

In my current example, I have an array and want to disable the elements in the array - but only if they have a property type that equals either ‘radio’ or ‘checkbox’. Looping through the array, this works:

var t = this.inputs[i].type || null; 
if(t==='checkbox' || t==='radio') { this.inputs[i].disabled=true; }

But it annoys me in this sort of cases that I have to type the variable’s name twice. I tried this instead which didn’t work:

t==='checkbox' || 'radio'

And this which worked in Firefox and Chrome, but not in IE8:

t==='checkbox' | 'radio'

Surely, there must be an shorter way of doing it than #1 above?


While I’m at it, I also find the if-else shorthand great:

(value) ? a=1 : a=2;

But it annoys me that it is not possible to do this as in a true shorthand for if without else:

(value) ? a=1; 
// or at least:
(value) ? a=1 : ;

It works with a hack, e.g. like this:

(value) ? a=1 : 0 ;

That’s fine although it looks ugly in the code. Can anyone tell me, why this is not possible then:

(value==='mystring') ? a=1 : 0 ;

This is where it gets odd. Example #2 above does not work entirely as intended. But elsewhere I have a similar case and it works like a charm:

  var keydown = function(event) {
    if (event.keyCode === 17 || 16) { // CTRL=17, SHIFT=16
      _this.toolState = _this.TOOLSTATE.TOGGLE;
  $(document).bind('keydown', keydown);

It responds perfectly to both the CTRL key and the SHIFT key.

The odd thing about #2 is that there was a object in that array which was not an HTMLElement and had no .type property at all [it was an Array(String)] - but it still “passed the test” of the if(t===‘checkbox’ || ‘radio’) and went into the following code block {}.

Anyone who can shed some light on this mystery?

if (event.keyCode === 17 || 16)

well, this is always true, because whatever event keycode is, 16 is a truthy value.

If you want to save typing, use the result of the test as the value of disabled:

var who=this.inputs[i] || ‘’, t=who.type;
who.disabled= t==‘checkbox’ || t=='radio;

// It does this: who.disabled=( t==‘checkbox’ || t===‘radio’)? true: false;

Ha ha - thanks! I forgot to test with any other keys than just ctrl and shift! It proves that no matter which key I press, it has the same effect - obviously, as you say, the number 16 always evaluates to true! Silly me … :expressionless:

And thanks for your other suggestion with " who.disabled= t==‘checkbox’ || t=='radio; " !