Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript isnull

This is a really great function written in jQuery to determine the value of a url field:

$.urlParam = function(name){
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    return results[1] || 0;
}

// example.com?someparam=name&otherparam=8&id=6
$.urlParam('someparam'); // name
$.urlParam('id'); // 6
$.urlParam('notavar'); // null

http://snipplr.com/view/11583/retrieve-url-params-with-jquery/

I would like to add a condition to test for null, but this looks kind of klunky:

if (results == null) {
    return 0;
} else {
    return results[1] || 0;
}

Q: What's the elegant way to accomplish the above if/then statement?

like image 614
Phillip Senn Avatar asked Dec 14 '09 20:12

Phillip Senn


1 Answers

return results == null ? 0 : (results[1] || 0);
like image 183
Brad Avatar answered Oct 14 '22 05:10

Brad