Strange Javascript Feature: (5,2) == 2 [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
The purpose of the comma operator in javascript (x, x1, x2, …, xn)
In Javascript (5, 2) gives 2, ('a', 'b', 'c') gives 'c' etc. (just try it out in the console).
My questions concerning that:
Is there a reason for that "feature"?
In which cases may it be useful?

You are looking at the comma operator.
The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.
You can use the comma operator when you want to include multiple
expressions in a location that requires a single expression. The most
common usage of this operator is to supply multiple parameters in a
for loop.

Related

What is this bracket sequence syntax called in JS? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 2 years ago.
I really don't know what to google to find out the name for this syntax:
(1,2) seems to evaluate to 2
(1,2,3,"cake") seems to evaluate to "cake".
etc
This is sometimes useful in anonymous functions in Array.reduce, where you need to perform a sequence of operations (say an increment) and also return the element on the right.
But where can I read about it and what's it called?
It's just the comma operator: it evaluates each of its operands (from left to right) and returns the value of the last operand.

Why in Javascript (1,2) equals 2? [duplicate]

This question already has an answer here:
What is JavaScript Comma Operator [duplicate]
(1 answer)
Closed 3 years ago.
I recently discovered that (1,2) equals 2
Same for (1,3,4) => 4 always return the last number
What is this expression exactly? It's not an array, not an object, what is it ?
Code within parentheses is evaluated as an expression. Several expressions can be grouped together by using a comma. Commas are not just syntax for dividing arguments or function parameters, they double as an operator - much like +, -, etc serve multiple purposes, so does ,
In the case you've outlined (1,2) the grouped expressions are obviously 1 and 2. They are both evaluated to themselves, but when expressions are grouped by comma operators, only the last evaluated expression is returned.
This means:
(1,2) == 2
and:
(1,2,3,4) == 4

JavaScript: parenthesis surrounding comma separated values [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 6 years ago.
I'm trying to get my head around the reason JS picks the last element (8):
var values = (null, 7, null, 8);
console.log(values);
// Output: 8
console.log and alert behaves the same so I assume it's something to do with the language itself.
From the first line of the docs:
The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.
This one also explained beautifully link
The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand

Comma separated number/function in parenthesis in JavaScript? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 6 years ago.
I read a line from doT.js:
var global = (function(){ return this || (0||eval)('this'); }());
After it was minified:
l=function(){return this||(0,eval)("this")}();
So what is the (0,eval), I mean what does the comma do?
I played in Chrome's console, (0,1), (2,1), (2,{}), 2,1, etc, it always returns the last one.
The comma operator evaluates both and always returns the last. Much like you said.
You can read up on the comma operator: http://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/
Even though I have no idea the purpose of (0||eval)... (0,eval) is the equivalent and one less character.

Meaning and function of ^ (caret) sign in javascript [duplicate]

This question already has answers here:
What does the ^ (caret) symbol do in JavaScript?
(5 answers)
Closed 6 years ago.
In a web page, I see following script snippet.
(d.charCodeAt(i)^k.charCodeAt(i)).toString()
It was a part from a for-loop, and I know what charCodeAt(i) is, but I really wondered what is the functionality of ^ sign... I make some search but failed to find anything...
What is ^ and what function or operator exists in Python or other programming languages that do the same job?
It is the bitwise XOR operator. From the MDN docs:
[Bitwise XOR] returns a one in each bit position for which the corresponding bits of
either but not both operands are ones.
Where the operands are whatever is on the left or right of the operator.
For example, if we have two bytes:
A 11001100
B 10101010
We end up with
Q 01100110
If a bit in A is set OR a bit in B is set, but NOT both, then the result is a 1, otherwise it is 0.
In the example you give, it will take the binary representation of the ASCII character code from d.charCodeAt(i) and k.charCodeAt(i) and XOR them. It does the same in Python, C++ and most other languages. It is not to be confused with the exponential operator in maths-related contexts; languages will provide a pow() function or similar. JavaScript for one has Math.pow(base, exponent).
In Javascript it's the bitwise XOR operator - exclusive or. Only returns true if one or the other of the operands are true, but if they're both true or both false, it returns false.
In Python, it does the same thing.
Wikipedia's XOR page.
It's a bitwise XOR. It performs an "exclusive or" in each bit of the operands.

Categories

Resources