totten
totten

Reputation: 2817

What is the ??! operator in Javascript?

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.

Upvotes: 1

Views: 2010

Answers (5)

Ahmad Nadeem
Ahmad Nadeem

Reputation: 31

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/

Upvotes: 3

Supr
Supr

Reputation: 19022

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.

Upvotes: 4

Xmindz
Xmindz

Reputation: 1282

Was this a mistake?

Did you mean this?

x= x?x:!x;

Upvotes: 2

Joey
Joey

Reputation: 354416

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.

Upvotes: 4

Quentin
Quentin

Reputation: 943220

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 :)

Upvotes: 12

Related Questions