In javascript I have the following:
var inf = id + '|' + city ;
if id or city are null then inf will be null.
Is there any slick way of saying if id or city are null make then blank.
I know in c# you can do the following:
var inf = (id ?? "") + (city ?? "");
Any similar method in javascript?
You can use lodash: _. isEmpty(value).
The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.
null is a special value in JavaScript that represents a missing object. The strict equality operator determines whether a variable is null: variable === null . typoef operator is useful to determine the type of a variable (number, string, boolean).
Description. A falsy value is something which evaluates to FALSE, for instance when checking a variable. There are only six falsey values in JavaScript: undefined , null , NaN , 0 , "" (empty string), and false of course.
How about:
var inf = [id, city].join('|');
EDIT: You can remove the "blank" parts before joining, so that if only one of id and city is null, inf will just contain that part and if both are null inf will be empty.
var inf = _([id, city]).compact().join('|'); // underscore.js
var inf = [id, city].compact().join('|'); // sugar.js
var inf = [id, city].filter(function(str) { return str; }).join('|'); // without helpers
Total long shot, but try this:
var inf = (id || "") + "|" + (city || "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With