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?

xhallix
  • 2,919
  • 5
  • 37
  • 55
  • 2
    [Why Use Semicolons?](http://stackoverflow.com/questions/2399935/why-use-semicolon) – jondavidjohn Mar 01 '13 at 19:00
  • JavaScript fundamental: Use semicolons, even when you think it's optional. – jbabey Mar 01 '13 at 19:02
  • 1
    There are many answers when you search StackOverflow for [javascript comma operator](http://stackoverflow.com/search?q=javascript+comma+operator) – the system Mar 01 '13 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. – the system Mar 01 '13 at 19:03
  • 1
    And [Comma operator](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Comma_Operator) at MDN. – Teemu Mar 01 '13 at 19:03
  • just found comma explanations which are not used for anonymous functions, so I asked to make sure I got it right – xhallix Mar 01 '13 at 19:11

4 Answers4

3

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

pdoherty926
  • 9,895
  • 4
  • 37
  • 68
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.

Teemu
  • 22,918
  • 7
  • 53
  • 106
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;
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
  • but why just need to use it on an anonymous function and what about the break? – xhallix Mar 01 '13 at 19:01
  • @ChristophHa Because the line break (sometimes) ends the statement. – ATOzTOA Mar 01 '13 at 19:04
  • so the break does quiet the same in this case? – xhallix Mar 01 '13 at 19:05
  • ok seems to be quiet complicated but thanks for you quick reply – xhallix Mar 01 '13 at 19:07
  • 1
    @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. – Teemu Mar 01 '13 at 19:42
0

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

G-Nugget
  • 8,666
  • 1
  • 24
  • 31
Amrendra
  • 2,019
  • 2
  • 18
  • 36