Is evaluation order consistent in Javascript?

At the interview, I was puzzled to hear that "javascript can evaluate applications out of order." To what extent is this true? I can imagine hundreds of ways to unambiguously evaluate statements of failure - for example, in a time-sharing operating system. But he seemed to say that if I value

console.log('a') console.log('b') 

that the Javascript specification somehow does not require the result to be a first, then b . I can imagine that the evaluator can try to evaluate the second operator if the IO of the first blocks, if the operators are functionally pure, that is, side effects, but side effects should always occur sequentially, right? And, of course, IO is one big side effect.

To what extent can specified Javascript be evaluated out of order? Or was it a case of misunderstanding?

+4
source share
4 answers

JavaScript is single-threaded (web workers aside). Period. ECMA-262 Language Specification Edition 5.1 says nothing about out-of-order execution. In your simple example, these two statements are guaranteed to execute in the same order.

In addition, one block of JavaScript code will never be interrupted by any other block of code, for example. event handler. This is why long blocks of code cause the user interface to freeze:

 for(var i = 0; i < 1000000000; ++i) { console.log(i); } 

This ensures that the code block above is never interrupted or reordered. While the loop is running, all event handlers and timeouts wait for one thread. And of course, the numbers will be displayed in the correct order.

What can be done out of order is an asynchronous timeout:

 setTimeout(function() { console.log('a'); }, 1); setTimeout(function() { console.log('b'); }, 1); 

Here you can expect a be printed first, but it is possible that the JS engine will change the order of these events. In the end, you plan to make these calls almost at the same time.

+7
source

Explicit misunderstanding.

Dude may have meant raising JavaScript. You can read more about this here: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting

In addition, here you can find out more features of the language: http://bonsaiden.github.com/JavaScript-Garden/

+2
source

For the most part, yes. With two main exceptions (leaving aside the obvious "definition of a function", call the function "which effectively" returns "to the body of the function):

1: Rise. var statements are raised, so if you write

 alert(a); var a = 123; 

You get undefined , not an error message. This is because he is raised to

 var a; alert(a); a = 123; 

Similarly, function definitions also rise. If you write:

 foo(123); function foo(num) {alert(num);} 

It will work because the function is raised. However, this does NOT work if you wrote

 foo(123); foo = function(num) {alert(num);} 

Because it is the purpose of an anonymous function, not the definition of a function.

2: Asynchronous functions.

A common mistake among beginners is to write the following:

 var a = new XMLHttpRequest(); a.open("GET","sompage.php",true); a.onreadystatechange = function() { if( a.readyState == 4 && a.status == 200) { myvar = "Done!"; } }; a.send(); alert(myvar); 

They expect a warning to tell Done! but instead they will get an inexplicable error because it is not defined. This is because myvar = "Done!" not yet run, despite appearing earlier in the script.


See also this joke from Computer nonsense :

An introductory student programmer once asked me to look at his program and find out why it always knocked out zeros as a result of a simple calculation. I looked at the program, and it was pretty obvious:

 begin readln("Number of Apples", apples); readln("Number of Carrots", carrots); readln("Price for 1 Apple", a_price); readln("Price for 1 Carrot", c_price); writeln("Total for Apples", a_total); writeln("Total for Carrots", c_total); writeln("Total", total); total := a_total + c_total; a_total := apples * a_price; c_total := carrots + c_price; end; 
  • Me: "Well, your program cannot print the correct results before they are calculated."
  • He: "Yeah, it’s logical that this is the right decision, and the computer must correctly reorder the instructions."
+1
source

I think they tried to put you on your wrong foot. Javascript is sequential. Otherwise, the functions in which you calculate the values ​​will not work. What may be true is that console.log activates the Async task , which means that it can be executed in a different order. Like calling Ajax , a Webworker , a timeout or interval .

If you do the following, it will result in B than A, this is not sequential code, as in the code. A comes for B, but B runs first.

 setTimeout(function(){ console.log("A") }, 5); console.log("B"); 
0
source

All Articles