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

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

Related

How to create a Regex expression that accepts an Empty String/ null values? [duplicate]

This question already has answers here:
Regular expression which matches a pattern, or is an empty string
(5 answers)
Closed 2 years ago.
I am trying to write up a regular expression that accepts numbers, decimals as well as an empty string / null value.
My current expression accepts decimal values that are dot separated. The numbers can start with either a - (minus) or + (plus). Additionally, the value can start with a decimal point - for instance: .56
Here is the expression:
/^([-+]?(\d+|\.\d+|\d+\.\d*))?$/
How can I refactor it to accept an empty string/ null value?
You can write something like this :
/^(?:[-+]?(\d+|.\d+|\d+.\d*))?$/
or maybe just append null using pipe:
/^(?:[-+]?(\d+|.\d+|\d+.\d*))?$/|null

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.

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

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

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.

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.

Categories

Resources