Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript expression in parentheses

Tags:

javascript

var x = (1,2,3);
alert(x);

This expression evaluates to 3.

How is this expression (1,2,3) called? Why does it return 3?

like image 839
DrStrangeLove Avatar asked Mar 05 '11 01:03

DrStrangeLove


People also ask

How do you use parentheses in JavaScript?

In JavaScript we only write a value in the parentheses if we need to process that value. Sometimes the purpose of the function is to perform a task rather then process some kind of input. Examples: var sayHello = function() { console.

What does () mean at end of JavaScript?

What does () mean at end of JavaScript? Adding the () to the end calls the function that was just created. In the case of this particular function, the anonymous function returns several properties to the Browser object.

What is an expresion in JavaScript?

JavaScript's expression is a valid set of literals, variables, operators, and expressions that evaluate to a single value that is an expression. This single value can be a number, a string, or a logical value as depending on expression.

What is double parentheses in JS?

It means that the first function ( $filter ) returns another function and then that returned function is called immediately. For Example: function add(x){ return function(y){ return x + y; }; } var addTwo = add(2); addTwo(4) === 6; // true add(3)(4) === 7; // true. Follow this answer to receive notifications.


2 Answers

Javascript has a comma operator, like C does. It evaluates each of the expressions, then returns the last one.

like image 183
Ned Batchelder Avatar answered Sep 28 '22 12:09

Ned Batchelder


I haven't seen this in Javascript before. But in a number of other C'ish languages, it basically evaluates each of the expressions in the parentheses and returns the value of the last one.

like image 35
cHao Avatar answered Sep 28 '22 12:09

cHao