4

I recently saw this piece of code on one of the scripts running on facebook:

__d("Shaka", [], function a(b, c, d, e, f, g) {
    c.__markCompiled && c.__markCompiled();
    var h = {};

For someone with C# as the main development experience background I couldn't help but notice that the c.__markCompiled && c.__markCompiled(); line looks odd atleast. I've coded quite a bit of javascript myself , but the only place I've used && was inside conditionals like ifs, for, while etc.. What does this statement do and what is the logic behind it?

iuliu.net
  • 6,666
  • 6
  • 46
  • 69
  • This is short form of `if(c.__markCompiled) c.__markCompiled();`. This form is very common in minified JS. – Alex Kudryashev Jun 18 '16 at 14:41
  • Well, here it definitely doesn't make any sense as it's neither conditional check nor assignment to a variable that may be reused. It's just executing some functions – Akshay Khandelwal Jun 18 '16 at 14:42
  • `&&` is just `logical AND` operator. When any left-to-right argument is falsy expession evaluation will stop. – vp_arth Jun 18 '16 at 14:44
  • 1
    Possible duplicate of [&& operator in Javascript](http://stackoverflow.com/questions/8720645/operator-in-javascript) – Sebastian Simon Jun 18 '16 at 14:47

2 Answers2

4

The line

c.__markCompiled && c.__markCompiled();

is effectively the same as

if (c.__markCompiled) c.__markCompiled();

The && operator only evaluates its right-hand side if the evaluation of the left-hand side is a truthy value (that is, not undefined, null, 0, "", NaN or false).

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • @iuliu.net: then it's not truthy. – Sergio Tulentsev Jun 18 '16 at 14:42
  • Yep, the original answer just got updated. Clear now. – iuliu.net Jun 18 '16 at 14:43
  • the semi-colon at the end signifies that the next statement is executed irrrespective of the if statement so your answer seems incorrect to me. Please elaborate. – Akshay Khandelwal Jun 18 '16 at 14:43
  • 1
    @AkshayKhandelwal: I think it's you who should elaborate on "seems incorrect to me". – Sergio Tulentsev Jun 18 '16 at 14:45
  • if you see the question, there's a semi-colon in the end indicating that even if the statement with && evaluates to falsy value, it'll still execute the statement below. So that does not make sense. Why would someone write it like this? is there a reason? – Akshay Khandelwal Jun 18 '16 at 14:47
  • The next statement IS executed irrespective of the if statement, as it's already outside of if's scope, as it's 2 lines 'away' from the if statement. – iuliu.net Jun 18 '16 at 14:47
  • 1
    @AkshayKhandelwal What this line does is essentially say "evaluate the following: c__markCompiled returns true and c.__markCompiled() returns true". Except that we're not doing anything with that evaluation. We don't care if both of these things are true. What we care about is the fact that if the left hand side of the expression fails, then the right hand side does not get executed. There is no need to execute it - we already know that the expression will return false. Therefore, the only way the function on the right gets executed is if the left hand side is true. – Matis Lepik Jun 18 '16 at 14:47
  • exactly my point. there's nothing conditional on that line. So It seems its used only for execution of the function or something. – Akshay Khandelwal Jun 18 '16 at 14:48
  • @AkshayKhandelwal: also note that there's no `if` statement in the original code. – Sergio Tulentsev Jun 18 '16 at 14:48
  • I understand that the `&&` is evaluating to if condition but not the reason behind why is it written that way? – Akshay Khandelwal Jun 18 '16 at 14:49
  • There could me many many reasons. Save space, use less instructions, make it more difficult (or easy) to read, you name it. – Sergio Moura Jun 18 '16 at 14:50
  • just `because`, may be? Or `because it can be written this way`? Don't forget it's minified code, no reason to save readability here. – vp_arth Jun 18 '16 at 14:51
  • @AkshayKhandelwal because it's shorter than the equivalents (`if (a) b();` and `a ? b() : null` and some people believe it's readable enough that the shorter syntax is worth it (especially if you end up using this pattern a lot, for example in a React app that renders stuff conditionally). – Matis Lepik Jun 18 '16 at 14:51
  • 1
    @AkshayKhandelwal there **is** a "conditional" on that line. The code checks to see that the object property exists before using it as a function reference and calling it. It's avoiding an `undefined is not a function` error. – Pointy Jun 18 '16 at 14:56
  • 1
    @Pointy: jesus christ. I noticed only now that the left side is not a call. :) – Sergio Tulentsev Jun 18 '16 at 14:57
1

The && operator is the same used on if statements.

If all elements in a if are compared from each other with &&, it will stop as soon as it hits an false value.

This will never get executed

false && some_function();

This will be executed every time

true && some_function();

You can even test one function and, if true, call something else

test_me() && call_if_true();

It's the same as:

if (test_me()) { call_if_true(); }

You can also go nuts:

test1() && test2() && some_thing() && something_else();

This last one will stop as soon as it hits a value that is equivalent to false.

Javascript is a beauty...

Sergio Moura
  • 4,888
  • 1
  • 21
  • 38
  • The term is "short-circuiting". And you're gravely mistaken. Javascript is one of the messiest languages known to man. :) – Sergio Tulentsev Jun 18 '16 at 14:46
  • Two "Sergio" with different oppinions... That's nice to have! Also, thanks for the "short-circuiting". It's good to learn new things every day! – Sergio Moura Jun 18 '16 at 14:48