1

when I'm looking for some sites Javascript code, I see this

function hrefLeftMenu() {
    var x = true;
    for (i in info) {
        $(".leftmenu ul").append("<li" + (x ? " class='active'" : "") + " onclick='openAnInfo(\"" + i + "\", this);'> - " + info[i].title + "</li>");
        x = x??!x;
    }
    openAnInfo("0", ".lelelesakineyy");
}

What it does in javascript? Why the coder's used this operator?

Thanks.

8
  • Strange i had never seen this, Seems like combination of some operators Commented Aug 30, 2012 at 12:34
  • Bit confused as to why this has four upvotes. The question requires more information. Is this the exact code you saw? How often have you seen it? Can you provide some of the surrounding code? Commented Aug 30, 2012 at 12:36
  • Are you sure about this? I had never seem such syntax! Commented Aug 30, 2012 at 12:37
  • 1
    What site did you encounter this on? Commented Aug 30, 2012 at 12:39
  • 1
    That's unfortunate. But does it throw an error at all? I'm not sure in what environment this code gets executed - perhaps a proprietary flavour of ECMAScript? Commented Aug 30, 2012 at 12:46

5 Answers 5

12

What it does in javascript?

It throws a syntax error.

> x = x??!x;
SyntaxError: Unexpected token ?

Why the coder's used this operator?

Taking a reasonable guess (beyond "they made a mistake") would need more context. Saying for sure would require mind reading :)

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

1 Comment

Why I did not check if it is works or not.. Thanks and sorry about that.
4

I think it is a mistake. They're generating a menu and x is used to set an item as active, and it looks like they want to default to selecting the first item. They want x to be true the first time around, and then false for the rest. It was probably supposed to be something like

x = x?!x:x; // If first time through, then set x = false for the rest

aka

x = false; // Set x = false for the rest

but confusion/muddy thinking led to over-complification.

Comments

4

In JavaScript this is not valid code. However, the sequence ??! exists (existed?) in C as a trigraph, representing |. Maybe that's not JavaScript code, or it was poorly-ported from ancient C code. But even then, x = x | x can hardly be called a useful statement.

EDIT: With a bit context in the question now, that speculation is likely wrong. A friend suggested that maybe the sequence x?? was a typo and subsequent attempt to correct it where backspace was turned into a character by some intermediate mangling (not uncommon when typing in terminals or via SSH) and that the line in question was supposed to be x = !x.

Comments

3

In JavaScript, the ?? operator is known as the nullish coalescing operator . It is used to provide a default value when the left-hand side operand is null or undefined. This operator is useful for handling cases where a variable may be null or undefined and you want to assign a fallback value.

Syntax:

let result = a ?? b;

a is the value to be checked. b is the fallback value if a is null or undefined.

Example:

let foo = null;
let bar = foo ?? 'default value';
console.log(bar); // Output: 'default value'

let baz = 0;
let qux = baz ?? 'default value';
console.log(qux); // Output: 0

How it Works:

If foo is null or undefined, bar will be assigned the value 'default value'. If foo has any other value (including 0, false, or an empty string), bar will be assigned the value of foo.

Difference from || (Logical OR) Operator The logical OR operator (||) also provides a way to fall back to a default value, but it treats other falsy values (0, false, NaN, '', etc.) as false and will use the right-hand side value in those cases.

If you want to read more: https://www.freecodecamp.org/news/what-is-the-nullish-coalescing-operator-in-javascript-and-how-is-it-useful/

1 Comment

This answer is actually the good one. However I would have quote MDN : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
2

Was this a mistake?

Did you mean this?

x= x?x:!x;

1 Comment

Without the operator, the whole x variable is pretty useless in the OP's snippet, so the ??! probably swaps the value or something. x?x:!x is likely not what the author meant.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.