Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript, string concatenation without ternary operator

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"!

like image 873
Fred Hors Avatar asked Dec 12 '25 17:12

Fred Hors


1 Answers

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

like image 57
sbrass Avatar answered Dec 15 '25 06:12

sbrass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!