Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript null value in string

Tags:

javascript

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?

like image 565
Nate Pet Avatar asked Apr 06 '12 15:04

Nate Pet


People also ask

How check string is null in JavaScript?

You can use lodash: _. isEmpty(value).

IS null == in JavaScript?

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.

IS null == null in JavaScript?

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).

Is empty string true in JavaScript?

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.


2 Answers

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
like image 123
psyho Avatar answered Oct 20 '22 00:10

psyho


Total long shot, but try this:

var inf = (id || "") + "|" + (city || "");
like image 40
Elliot Bonneville Avatar answered Oct 20 '22 00:10

Elliot Bonneville