7

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?

2
  • Your question is a bit confusing. Please provide one ore more example values for inf. Commented Apr 6, 2012 at 15:52
  • possible duplicate of null coalescing operator for javascript? Commented Apr 6, 2012 at 16:26

7 Answers 7

22

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
Sign up to request clarification or add additional context in comments.

3 Comments

inf will not be blank , if id and city are nulls.
@Engineer: Yes, it'll be "|", the desired result.
@Rocket I know about "|", just read if id or city are null then inf will be null. of @Nate.
16

Total long shot, but try this:

var inf = (id || "") + "|" + (city || "");

13 Comments

I'd suggest adding parenthesis: var inf = (id || "") + "|" + (city || "");
@ElliotBonneville it doesn't actually matter to the interpreter (since it's equivalent), but it will matter to your co-workers.
@nickf It does make a difference. Try it.
@Nate: It's an 'or' operator, which means this: var inf = (id if id is not undefined, otherwise use "") + "|" + (city if city is not undefined, otherwise use "")
@EliranMalka: This works for any value evaluating to false. So also 0, which might not be wanted.
|
5
var inf = (id == null ? '' : id) + '|' + (city == null ? '' : city)

3 Comments

That's just a longer way to do what Elliot is doing in his answer
I have no idea why this was accepted instead of pysho's answer.
Sometimes longer and easier to read and understand answers are better than shorter and cryptic ones. As long as it's not wrong, why do people care.
1
function nullToStr(str) {
    return !str || 0 === str.length ? '' : str;
}

usage:

console.log(nullToStr(null);

        function nullToStr(str) {
    return !str || 0 === str.length ? '' : str;
}

console.log(`The value which was null = ${nullToStr(null)}`);
console.log(`The value which was null = ${nullToStr(undefined)}`);
console.log(`The value which was null = ${nullToStr('')}`);
console.log(`The value which was null = ${nullToStr('asdasd')}`);

I think it will help and is much easier to use. Obviously, it is based on the other answers i found in this thread.

Comments

0
var inf = (id && city) ? (id+"|"+city) : "";

2 Comments

What if one or the other is null? He wanted each variable replaced with "" if it was null, not the whole thing.
@Rocket "if id or city are null then inf will be null", I think he wants empty string either one is null.
0

Equivalent to c# var inf = (id ?? "") + (city ?? ""); (if id and city are nullable) is

var inf = (id || '') + (city || '');

This is referred to as 'Short-Circuit Evaluation'. Nullability is not an issue in javascript (in js all variables are nullable), but id and city have to be assigned (but do not need a value, as in var id, city).

Comments

0
if (id != ""){
inf = id;
if (city != ""){ inf += " | " + city ;}
}
else 
inf= city;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.