2

I found this line of code in a project and I couldn't under

return d.isPointInside(a, b) ? (console.log("runned"), c = !0, !1) : void 0

And I couldn't understand the part:

(console.log("runned"), c = !0, !1)

The word "runned" should be displayed in the browser's console, But need some help to understand the rest.

2
  • This may help to understand Commented Sep 4, 2016 at 9:15
  • This question is similar to: What does the comma operator do in JavaScript?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 1, 2024 at 11:17

3 Answers 3

4

Just some type juggeling

!0 === true
!1 === false
void 0 === undefined

The result with some longer words:

(console.log("runned"), c = true, false);

The complete return statement reads as following, without use of the Comma Operator:

if (d.isPointInside(a, b)) {
    console.log("runned");
    c = true;
    return false;
} else {
    return undefined;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, these responses were very useful.
2

The commas just create a sequence, so ultimately this statement will return !1 which is false. The code is exploiting the sequence syntax to produce a byproduct of outputting to console and setting a variable.

It's similar to:

var a,b,c;

Which declares variables but doesn't do anything with them. This is equally valid:

var a=7,b=someComplexCalculation(),c;

!1 is false, and is kind of useless, but in JavaScript this is a useful form because it forces type to be Boolean. When developing, it may be useful to change !1 to !a, which is guaranteed to return a Boolean value.

The last value of the sequence completes the statement, and acts as the return value.

So:

X=(3, Math.sqrt(2), "something");

Will set X="something"

1 Comment

this statement will return !1 which is false you should expand on your answer.
1

First, the code is using the comma operator in JavaScript that evaluate everything but only return the last element. For example var a = 1, 2, 3; will always be equal to 3 because only the last item gets returned.

With this in mind, let's look at the piece of code you didn't understand:

(console.log("runned"), c = !0, !1)

Due to the comma operator, this breaks up into three expressions that will be evaluated in a sequence:

console.log("runned")

Will print the given word in the console.

c = !0

Will assign the value of true to the variable c. This is because zero is a falsey value which when converted to the opposite using the not operator ! produces true.

`!1`

Finally the result of this expression will be returned from the entire section inside the brackets. Similar to the above, this uses the fact that 1 is a truthy value, so !1 will yield false.

So, taken as a whole, the code will print, assign a value, and return a value all in one line.

Given the syntax and the variables used, my assumption is that this is minified code. Minification uses the language rules in a similar manner in order to reduce the amount of characters used. In partucular !0 and !1 are very common replacements of true and false because they latter are at least half the length.

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.