Use && to simplify js codes

The technique will be illustrated by the following simple js codes:

js-reg-and.jsview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var s='data0', m=/^([a-z]+)(\d+)$/.exec(s);

/* the most common (and obvious) way to
* check if there is match and the 2nd captured group's value is 0
* would be
*/
if(m) {
if( m[2] === '0' ) {
console.log('maybe');
}
}


/* but the above code can be simplified to */

if( m && m[2] === '0') {
console.log('maybe');
}

/* the technique can also be used in switch-case statement */

switch( m && m[2] ) {
case '1':
console.log('never');
break;
case '0':
console.log('maybe');
break;
default:
console.log('run out of options')
}