javascript null value in string

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?


var inf = (id == null ? '' : id) + '|' + (city == null ? '' : city)

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

总远射,但试试这个:

var inf = (id || "") + "|" + (city || "");
链接地址: http://www.djcxy.com/p/73376.html

上一篇: XML解析。 我怎样才能得到孩子的孩子?

下一篇: 字符串中的javascript null值