Preliminary
When one subscribes to queryParams like below, the params variable holds an object where the keys are all the query paramaters and the value can either be a single value or an array of values. One can do a console.log(params);
to check this result. For example:
when url is: http://example.com/?lion=10&lion=15&cat=20
and we then try to see what params holds:
this.route.queryParams.subscribe((params) => {
console.log(params);
});
the result would be an object where keys are lion
and cat
but values are an array for lion since it has two occurrences and for cat is a single value:
{ lion: [10, 15], cat:20 }
Now answering the question
Let's take the url to be: http://example.com/?id&name=james
One can notice that there is no value for id. It is possible that a url just has a key but no value and yet we would like to know that the key at least is present. If we were to do:
if(params['id']){
// do something
}
this condition will return false even though the key id is present. We might actually want to do something when the key alone is present and something else when the key and value are present. In that case we can use lodash library to effectively sort key and key/value results. After importing lodash library in to the project. One can use _.has to do this:
// check if id key is present in params object first
if(_.has(params, 'id')) {
if(params['id']=="") {
// id key is present but has no value so do something
} else {
// id key is present and has a value as well so do something else
}
} else {
// since id key itself is not present do something totally different here
}
Of course the above code assumes that id key appears only once in the url params. Otherwise, one would have to loop through an array and check each value.