3

Usually I use this:

myVar = "myString is" + 1 === 1 ? " really true" : " false, I think";

Maybe I need just the true part, let's say:

myVar = "myString is" + 1 === 1 ? " really true" : "";

I don't like this part: : "" because is useless.

Is there a way to use something like the below?

myVar = "myString is" + 1 === 1 && " really true";

It works but there is a problem when is false because it writes "false"!

7
  • 1
    "myString is" + 1 === 1 && " really true" is evaluated as ((("myString is" + 1) === 1) && (" really true")). "myString is" + 1 is "myString is1", which is not equal to 1. Commented Aug 1, 2018 at 18:40
  • Yes there is, wrap that 1 === 1 && " really true" inside parentheses () like I've shown in my answer, but will still write something like myString is false which I don't know if it's acceptable to you or not. Commented Aug 1, 2018 at 18:46
  • @ionizer he's quite clear (in bold in fact) that writing false is a problem. Commented Aug 1, 2018 at 18:46
  • 2
    Personally, I think the ternary with the "unnecessary" "" is the way to go. Commented Aug 1, 2018 at 18:48
  • 1
    I agree with @MarkMeyer. If it needs to be inline, adding implicit boolean conversions and comparisons isn't going to be any cleaner than the ternary Commented Aug 1, 2018 at 18:49

6 Answers 6

1

You could always go with a good old if statement

var myVar = 'myString is';
if (1===1){myVar+=' really true';}

I think that makes it more readable than a one line boolean test

Sign up to request clarification or add additional context in comments.

2 Comments

I need it inline.
@Fred Hors So is a theoretical game or a programming question ? I don't want to troll, I would like to understand the ballpark of the question.
1

You can just use ||

myVar = (1 === 1 && "myString is really true") || "";

4 Comments

That seem no better (and a little worse) than the original ternary.
@MarkMeyer Just curious. In what way ?
The OP wants to concatenate something to the original string. This set myVar to "" when the condition is false.
For example I need it for a css class. I have a "btn w-100" and then all the javascript logic just if true.
1

Wrap the 1 === 1 && " really true" inside parentheses () and add || '' like below (also wrapped in parentheses), or could use template literals to save you some time from typing those +s

let myString = "myString is" + ((1 === 1 && " really true") || '');
let myFalseString = "myString is" + ((1 === 0 && " really true") || '');
let onlyFalse = "myString is" + 1 === 1 && " really true";
let myTL = `myString is ${(1 === 1 && "really true") || ''}`;

console.log('My String:', myString);
console.log('False String:', myFalseString);
console.log('Only false:', onlyFalse);
console.log('My Template Literal:', myTL);

Looks much worse than having the extra : "" though so I would still recommend doing it like that:

myVar = "myString is" + 1 === 1 ? " really true" : "";

2 Comments

This will still write false to the string in the case of false tests, which OP points out and doesn't want
Updated answer to not write false any longer, except for the onlyFalse which is provided for demonstration. Way too hacky though.
0

To be pragmatic the best pattern and best way to write this is to rely on a helper:

myVar = "my string is"+myHelper(...myParams);

Then in the helper we'll have a case/switch that is made exactly with this purpose and is really readable.

Comments

0

another way to achieve something like this could be to use an Array, and concat the values if they are not false. Not that it is any shorter than adding the : '', but as far as i know there is no way to get rid of the : ''

console.log( ["my string is", 1 === 1 && "really true"].filter(Boolean).join(" ") );
console.log( ["my string is", 1 === 2 && "really true"].filter(Boolean).join(" ") );

i would prob stick with the : '' or write a helper function that could look something like this.

function concat(){
  let str = "";
  for(let s of arguments){
    str += s ? s : '';
  }
  return str;
}

console.log( concat("my string is", 1 === 1 && "really true") );
console.log( concat("my string is", 1 === 2 && "really true") );

Comments

0

Analysing the ternary operator we conclude that it's something like this:

// Example: 1 === 1 ? ' is really true' : ''
if (1 === 1) {
    return ' is really true';
} else {
    return '';
}

So the solution would be simply to remove the 'else' from the ternary operator, generating a 'binary'. Use a lone IF:

if (1 === 1) {
    myVar += 'is really true';
}

The best solution to use the logic operator inline is the ternary operator itself. It's not a problem to have the 'false' part of it as a empty string "". But if you're really annoyed by it you could create a function and use the template literals like this:

function myFunction(evaluation) {
    if (evaluation) {
        return ' really true';
    }
    return '';
}

let myVar = `My String is ${myFunction(1 === 1)}`;

1 Comment

I can use it inline?

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.