0

I know that var some = []; creates a new array and var someother = {} creates a new object. So what does the () do?

Specifically, in the following code :

var someVar = (1,2,3); //someVar equals 3

and

typeof (1,2,3) //returns "number"

So what does the () do in the above code?

2 Answers 2

1

See precedences of operators.

(1,2,3) 

is just

3

because the comma operator returns the last value.

The parenthesis in

typeof (1,2,3)

just prevent it to be interpreted as

(typeof 1),2,3

because the typeof operator has a higher precedence than the comma operator.

Sign up to request clarification or add additional context in comments.

Comments

0

That is a grouping operator.

It causes the contents to be evaluated as an expression in their own right. Effectively it changes precedence since otherwise the someVar = 1 would be evaluated first (instead of the result of (1,2,3) being evaluated and the result used in the someVar = ... expression.

That expression 1,2,3 uses a comma operator which evaluates as the right hand side, so it is 3.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.