-1

For example,

var doSomething = function() {`
    var a = 1, b = 2, c = 3;
    var d = a;
    b = b - 3;
    d = b;
    console.log(a,b,c,d);
}

doSomething();

Is there any possible situation beginning from the dawn of the universe until the end, that the output of the code when executed in node js - built on JavaScript's V8 engine would be anything other than 1 -1 3 -1.

Is there any combination of synchronous lines of code that do no execute in the order they are written.?

Any puns are not welcome and any help is appreciated.

Additional question:

var index = commonFunc.getObjectIndexFromArray(option[0].fields.custom_field, tableAccess.checkDomainInTable.selected_template); 
if (option[0].fields.custom_field && option[0].fields.custom_field.length > 0 && index >= 0) {
    userOptions = option[0]['fields']['custom_field'][index][tableAccess.checkDomainInTable.selected_template]; 
} 
if(tableAccess.checkDomainInTable.delivery_template) { 
    index = commonFunc.getObjectIndexFromArray(option[0].fields.custom_field, tableAccess.checkDomainInTable.delivery_template); 
    if (option[0].fields.custom_field && option[0].fields.custom_field.length > 0 && index >= 0) {
        deliveryOptions = option[0]['fields']['custom_field'][index][tableAccess.checkDomainInTable.delivery_template]; 
    } 
} 

In the above code, after assuming that the common function gets the index of a json object from an array of json object using simple for loop, can there be any situation in which the second occurence of the assignment of the variable index be executed first and the first occurence after that & causes an ambiguity in the values of userOptions and deliveryOptions ?

wmk
  • 4,598
  • 1
  • 20
  • 37
alpheus
  • 240
  • 1
  • 12
  • This similar post might give more information http://stackoverflow.com/questions/23392111/console-log-async-or-sync – Abhinav Galodha Feb 04 '17 at 16:51
  • Why are you asking, out of curiosity? That is the definition of "synchronous". –  Feb 04 '17 at 17:08
  • We can't figure out what you're asking. Of course, statements are executed in sequence. That goes without saying. What makes you think they might not be? By the way, is there some particular reason you are repeating `option[0].fields.custom_field` over and over in your code instead of pre-calculating it? –  Feb 04 '17 at 21:55
  • I was told by someone that in some case, the statement below would be executed before the former and that in that case the userOptions and deliveryOptions would have the swapped values instead of what they should be. Another person told me that it would give correct results in most of the cases and in some machines it would not. I didn't believe any of it and was just confirming what I knew to be true. There is no particular reason for evaluating `option[0].fields.custom_field` again and again and yes it would be better if they are precalculated. – alpheus Feb 05 '17 at 05:26

1 Answers1

1

If that's the entire code, and assuming no bugs in the V8 implementation or the underlying hardware - no, this code will always execute in the same order and have the same results (that would be those that you provided).

obe
  • 7,378
  • 5
  • 31
  • 40