What’s the best way to handle javascript isnull in this function?

I have a function that retrieves URL parameters using jQuery:

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

When I call it like this:

$.urlParam('someparam'); // name
$.urlParam('id'); // 6
$.urlParam('notavar'); // null

I want to add a condition to check for null values, but it feels a bit clunky:

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

How can I elegantly handle this check for null in JavaScript?

Hey buddy! In my opinion, using the Logical OR (||) for Fallback is your solution.

You can simplify the null check by using the logical OR operator to provide a fallback value directly when the result is null or undefined:

javascript

CopyEdit

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

Why it works:

  1. The results ? results[1] : 0; check is a cleaner and direct way to return results[1] if it exists, and 0 if results is null or undefined.
  2. The ternary operator gives you a concise check while still handling the fallback scenario smoothly.

I see @jacqueline-bosco has added an answer already, but I don’t think one extra trick up your sleeve ever hurts. And here’s what I do for the same:

This approach is called Short-Circuit with Logical OR (||) — it’s very straightforward but ensures results[1] is returned as 0 if it’s falsy (i.e., null, undefined, or any falsy value):

javascript

CopyEdit

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

Why this works:

  • This method ensures that if results is null or undefined, it immediately short-circuits to 0.
  • The key difference is that results[1] will only be used if it’s truthy, which may be more appropriate if you don’t want to return other falsy values like false, 0, or an empty string.

That’s my go-to move for clean fallback handling!

I agree with both @netra.agarwal and @jacqueline-bosco — their ways would work just fine for sure. But hey, I’m here with a little present for you (another super cool method).

You can use ?? (Nullish Coalescing Operator) for a cleaner, modern approach.

If you’re using a modern version of JavaScript (ES2020+), this lets you handle null or undefined values specifically:

javascript

CopyEdit

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

Why this works:

  • The ?. (optional chaining) ensures you only access results[1] if results is not null or undefined.
  • The ?? 0 part will return 0 if results[1] is null or undefined, making it more explicit than the || operator, which would also coerce other falsy values (like false, 0, or '') to 0.

That’s it — neat, clean, and modern! Adios!