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.
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.
Share Improve this question edited Aug 30, 2012 at 12:37 totten asked Aug 30, 2012 at 12:31 tottentotten 2,8173 gold badges28 silver badges45 bronze badges 8- Strange i had never seen this, Seems like combination of some operators – sushil bharwani 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? – jackwanders Commented Aug 30, 2012 at 12:36
- Are you sure about this? I had never seem such syntax! – Xmindz Commented Aug 30, 2012 at 12:37
- 1 What site did you encounter this on? – pimvdb 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? – pimvdb Commented Aug 30, 2012 at 12:46
5 Answers
Reset to default 12What 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 :)
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.
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
.
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/
Was this a mistake?
Did you mean this?
x= x?x:!x;