Jump to content


Photo

understanding semicolon insertion

JavaScript

  • Please log in to reply
No replies to this topic

#1 ancit

ancit

    Member

  • Members
  • PipPip
  • 10 posts

Posted 31 December 2012 - 06:56 AM

If there is a new line, and we have a valid statement we use a semicolon after statement  ?

 

What is a statement ?

x=x + "The number is " + i + "<br>";i++;
var varibale = 5;
var varibale = { };
var varibale = function(){};
alert("bar");
return{};

 

In some case semicolons are optional in JavaScript,

ECMAscript trys to be smart and will automatically insert semicolons if you didn't write,
Therefore we can get unexpected resualt;
What do you think the output will be?
function foo(){
    return // Bad!

    {
        name: "Jo"
    };
}
var bar = foo();
​alert​ (bar.name)​;​

 

The output return :

undefined

 

Because when we run the function we get

 

return;

 

// Good
return {
name : "jo"
};

 

 

 

 

 

 

 







Also tagged with one or more of these keywords: JavaScript

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users