It errors when it doesn't find a match, so let's deal with that issue instead. We can get the match separately, and the check to see if the match contains anything useful before getting the [1] index from it. If it doesn't contain anything useful, we can give it a default value of an empty string instead.

You're welcome. Just to explain briefly, this is where the syntax comes from for:

where = (match && match[1]) || '';

First we start with how that might be written in full, as:

if (match.length > 0) {
where = match[1];
} else {
where = '';
}

Which can then easily be turned in to a ternary expression of the format (...) ? ... : ...;

where = (match.length > 0) ? match[1] : '';

And that might do just well, except that it's not being expressive enough. Currently the above code says that the where variable is either an array value, or an empty string. But it doesn't really make it clear as to why this should be the case. Sure, we can come up with some kind of correlation between the length check and the match[1], but there are more expressive ways to go about this.

What we can do is to use the && operator as a guard condition. Only if the preceeding condition is truthy, will JavaScript be allowed to carry on and check the next one. This is something that is commonly used to check if something exists first before using it.

if (targ && targ.nodeName && targ.nodeName === 'A') {
...
}

And there is also the || operator which is used as a default value, because JavaScript will keep on checking the different conditions until it comes across one that is truthy in nature. This is commonly used to assign default values to a variable:

So we can put the two together. To say that match[1] is being guarded first, in case it doesn't exist, and if nothing is found there, that a default value of an empty string should be used instead.

where = (match.length > 0) && match[1] || '';

Now since an array is considered to always be a truthy value, even if the array is completely empty, and a failed regular expression match doesn't give an array, but null instead, we can just test to see if the match is truthy or not.

where = match && match[1] || '';

And finally to help make things clearer to someone who is reading the code, we can use parenthesis to help clarify that the first two parts are related to each other:

where = (match && match[1]) || '';

So you could have ended up with a whole lot of if/else statements in your code.

if (match.length > 0) {
where = match[1];
} else {
where = '';
}

But you now have in return code that is more expressive instead. I wouldn't say that this is a one-liner, because reducing code to a single line is not a good goal to have. Instead, it makes use of some well known javascript techniques to result in being even more expressive than the if/else code from before.

where = (match && match[1]) || '';

sabret00the
—
2012-08-18T21:40:21Z —
#9

Thanks again Paul. Me and JS have never gotten along that well, but you're actually amazing at explaining this stuff. Truly, thank you.

Paul_Wilkins
—
2012-08-18T21:46:19Z —
#10

sabret00the said:

Thanks again Paul. Me and JS have never gotten along that well, but you're actually amazing at explaining this stuff. Truly, thank you.