2

I was reading an article that was talking about conditional javascript but they didn't explain which was better to use. I made a fiddle with the 2 examples.

console.log('start');

const VALUE = true;
const TEST = false;

//test 1
VALUE && !TEST && (() => {
    console.log('hello there!');
})();

//test 2
if(VALUE && !TEST) {
    console.log('bye');
}

https://jsfiddle.net/xvdLq6to

The most important thing with this example is that I don't want to call a new function I want to execute the code in the function its inside just like an if statement. If you know something better then I can't wait to hear that!

Here is the article (keep in mind that the example is not directly from the article) https://hackernoon.com/conditional-javascript-for-experts-d2aa456ef67c

Rick Grendel
  • 303
  • 1
  • 4
  • 14
  • 6
    The `if` looks far better IMO, using a construct that results in an expression (including the conditional operator) are probably best used when you need the *result of the expression*. If you're just using boolean logic, use `if` / `else` – CertainPerformance Nov 20 '18 at 08:31
  • Agreed with @CertainPerformance. I've never seen the "test 1" kind used anywhere, and don't know what its benefit would be. – RobertAKARobin Nov 20 '18 at 08:34
  • 3
    Personally I wouldn't bother with the syntax shown in test 1. Readability of your code should be higher priority here. Also a tool like an uglifier or minifier can probably generate code pretty similar to test 1 for you automatically while not sacrificing readability. – Mathyn Nov 20 '18 at 08:34
  • 1
    If you're worried about the function, don't be, just leave it off entirely `VALUE && !TEST && console.log('hello there!');` – CertainPerformance Nov 20 '18 at 08:38
  • Can you please link the article that you read? – Bergi Nov 20 '18 at 08:54
  • @CertainPerformance yeah this is just an example I would want to use this with multiple lines of code inside not only the log ;) – Rick Grendel Nov 20 '18 at 08:59
  • @Bergi I linked the article in the question above – Rick Grendel Nov 20 '18 at 09:02
  • @Bergi I would also like to clarify why this is not a duplicate the big difference here is the anonymous function. This is more a ''alternative" of an if statement inside the function instead of calling another function what happens in the other question. – Rick Grendel Nov 20 '18 at 09:06
  • 1
    You say they weren't explaining which was better to use, but I can find "*you will see how some conditional statements can be converted to simple expressions. You will also see how such conversions can make your code look more compact and cleaner.*" Of course, many would disagree - `if` is much more readable for a statement with a side effect. Yes, they can shorten it, but the article is called "*conditionals for experts*" not "for minifiers". It only makes sense when you want to produce a value (typically with a ternary). – Bergi Nov 20 '18 at 09:10
  • @RickGrendel The IIFE doesn't really make a difference - it's just another expression to be evaluated. And it doesn't make the code better anyway... – Bergi Nov 20 '18 at 09:16

1 Answers1

4

The second example should be the preferred syntax, by far. Readability is incredibly important and the second example is considerably more readable, it's easier to understand the intention of the code.

The first example, while technically possible, is simply not good code, in my opinion.

Flater
  • 12,908
  • 4
  • 39
  • 62