0

While I was trying some fundemantels in Javascript, I came across a question, which suprised me, and I cannot find an answer to that. I got the following, which works perfecty:

var obj= new Object ();
    obj.test = "Hello" 

    obj.testTwo= function (){
        console.log(this.test)
    },obj.testTwo();

When I'm trying it without a comma, it does not work.

var obj= new Object ();
    obj.test = "Hello" 

    obj.testTwo= function (){
        console.log(this.test)
    }obj.testTwo();

So I tried this as a third option...and it works?

var obj= new Object ();
    obj.test = "Hello" 

    obj.testTwo= function (){
        console.log(this.test)
    }
    obj.testTwo();

Now I'm quiet confused. Why to use a comma and why does it work with a break?

6
  • 2
    Why Use Semicolons? Commented Mar 1, 2013 at 19:00
  • JavaScript fundamental: Use semicolons, even when you think it's optional. Commented Mar 1, 2013 at 19:02
  • 1
    There are many answers when you search StackOverflow for javascript comma operator Commented Mar 1, 2013 at 19:02
  • 1
    @jbabey: That's not a JavaScript fundamental. A JavaScript fundamental is ASI, and the developers can decide for themselves if and when to take advantage of it. Commented Mar 1, 2013 at 19:03
  • 1
    And Comma operator at MDN. Commented Mar 1, 2013 at 19:03

4 Answers 4

3

What you're seeing is the comma operator in action. https://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/

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

Comments

2

In the first snippet, the comma operator does its duty as it is documentated: "evaluates both of its operands (from left to right) and returns the value of the second operand." MDN.

Basicly your second snippet is an assignment. Assignments should always be terminated with a semicolon, even if their last expression would be a block of statements. However, this is not obvious to ASI. Hence this snippet fails without either a semicolon or a newline between the block and object method call, where interpreter expects to see an operator or a termination of the assignment. If none of these is found, an Unexpected token error is thrown. This same explanation stands for why the third snippet works.

Comments

0

The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.

So, it evaluates both your codes. But it doesn't when you put them together.

Read more...

Example:

It is same as when you do

var a = 10, b = 40;

5 Comments

but why just need to use it on an anonymous function and what about the break?
@ChristophHa Because the line break (sometimes) ends the statement.
so the break does quiet the same in this case?
ok seems to be quiet complicated but thanks for you quick reply
@ATOzTOA To be exact, comma operator in var is not a good example here. At the MDN page you've linked, they say: "Note that the comma in the var statement is not the comma operator...". Rather this is one of those rare cases, when ASI can't do the job correctly.
0

Newlines (\n or \r\n) and commas (,) acts as statement separators.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.