2

WIKI:

In computer programming, a statement is the smallest standalone element of an imperative programming language that expresses some action to be carried out. It is an instruction written in a high-level language that commands the computer to perform a specified action.

W3C

Semicolons separate JavaScript statements. Add a semicolon at the end of each executable statement

pretty much resumes what i thought, but i would love to share some thoughts with you guys on these situations.

case 1:

var MyPromise = GetPromise(); //first statement
MyPromise.then(function(){

}); //second statement

case 2:

GetPromise()
.then(function(){

}); //first statement

Wiki source claims to consider high level languages when talking about statements (as reported)

It is an instruction written in a high-level language..

actually i don't know how trustable the source is but if i consider the high level javascript code (not the machine code generated by the interpreter) should there be any reason to choose one in place of the other (rather then readability)?

Karim
  • 8,454
  • 3
  • 25
  • 33
  • `GetPromise(then, catch)` ¯\\_(ツ)_/¯ – GottZ Jun 27 '17 at 14:05
  • 1
    little explaination of what `.` does: it refers to the object returned by the previous expression. such an expression can be a function execution aswell as a variable or definition. the dot simply operates on the object that got returned. thats it. (and yes, even `.then()` returns the promise object. you could essentially do: `.then().then().then()` – GottZ Jun 27 '17 at 14:07
  • *a statement ends with a ;* => alert("never ending... Weve got ASI") – Jonas Wilms Jun 27 '17 at 14:07
  • From Wiki: *A statement may have internal components (e.g., expressions).* or statements. A while statement contains a block statement contains other statements contain expressions contain... – Jonas Wilms Jun 27 '17 at 14:12
  • 1
    The difference between your codes is that one stores the promise in the global window object while the other one can never be reused => will may be garbagge collected somewhen. – Jonas Wilms Jun 27 '17 at 14:15
  • @Jonasw, yes this is definetely something i didn't think about, but apart from that is there any other difference performance wise in using one or more statements? – Karim Jun 27 '17 at 14:23
  • @Karim dont think about that. If you want performance, write your codes in assembly – Jonas Wilms Jun 27 '17 at 14:27
  • `1+3`, `3+1` and `2+2` are different terms as well and still produce the same result. – Bergi Jun 27 '17 at 16:56

1 Answers1

1

Semicolons separate JavaScript statements

No, thanks to ASI we dont need semicolons ( in most cases, and its still better to use them):

alert("two")
alert("statements")

So semicolons is not the way to go. Lets take this case

alert(test)

How many statements are there? one or two? Because:

test

is a statement too. To be precisely its an expression statement . But its common language use to just call them expressions.[1] The number of statements ( that are not expression statements ) can be varied freely, e.g:

a();
b();

"vs."

a(),b();

I assume then that the statement flows can be somehow controlled by the programmer.

Yep. Called programming.

should there be any reason to choose one in place of the other (rather then readability)?

Readability! Theres no difference between two statements or one statement containing two expressions.

However in your case:

var MyPromise = GetPromise(); 
MyPromise.then(function(){
});

MyPromise is stored in the window object, therefore it wont be recycled somewhen. So you tell the parser that you may want to use it somewhere else. If you dont, it does not make sense to store it in window.

1:What is the difference between an expression and a statement?

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • actually ASI is not a life saver, in some situation an explicit semicolon is needed in order to prevent an error. https://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript – Karim Jun 27 '17 at 14:58
  • @Karim *and its still better to use them* – Jonas Wilms Jun 27 '17 at 14:58