What does the "do" function do in JavaScript?

12

I searched a lot and could not find the functionality of do in JavaScript. Example:

function b(a, b) {
    do a = a[b]; while (a && 1 !== a.nodeType);
    return a
}
    
asked by anonymous 07.07.2017 / 15:42

4 answers

12

The same as in other languages, it's just a flow control. It is a marking for a loop to know where it begins. It is considered a label . Note that it is not a function, it is a keyword that determines what the code should do, we can say that it is a command.

It is used in a while that we can call inverted. That is, first enters the loop, without any condition, executes all of it and in its end is that a check is made whether to continue or not repeating this loop. If the decision is to continue the repetition and return exactly to the do line. So the loop will always be executed at least once.

Without it, using while normal, it may be that even the first time it is not executed, if the condition is false.

You can live without it, but the code can get more confusing, not serious, but you may have to juggle or use a flag variable for the first time to run .

The keys have been omitted because the code block has only one line, so it's a simplified form. In general it is best to avoid this style, it becomes more difficult to read. It could do in a row with braces:

do { a = a[b]; } while (a && 1 !== a.nodeType);

Easier to understand:

do { //marca o ínico do laço
    a = a[b];  //faz o que tem que fazer
} while (a && a.nodeType !== 1); //só agora verificará se deve repetir

Without do :

a = a[b];
while (a && a.nodeType !== 1) { //a condição é a mesma, só é executada em outro momento
    a = a[b];
} //aqui marca o fim, certamente voltará para o início para decidir se continuará

Whenever you want to know something about JavaScript at MDN can be considered the official documentation .

    
07.07.2017 / 15:45
9

This is simply a loop / loop do while , but with only one line of code then it does not take the keys { and } .

Normally it would be written like this:

do {
    //codigo a repetir no laço/ciclo
} while (condicaoDeFim);

If you only have one line you can write like this:

do /*unica linha de código*/ while (condicaoDeFim);

Soon the code presented could be written like this:

do {
    a = a[b]; 
} while (a && 1 !== a.nodeType);
    
07.07.2017 / 15:45
5

Just complementing the other answers, and to help in understanding:

do /*faça algo*/ while /*enquanto uma condição seja verdadeira*/

:)

    
07.07.2017 / 15:50
2

The do function must precede the while . It serves to run the code once at least if the condition of the while is false.

I've put examples to demonstrate the use of do .

  

Documentation link

Example:

var i = 0;

// Exemplo onde será executado normalmente até que a condição seja falsa e pare
do {
  console.log("i é igual a : %d", i);
  i++;
} while (i < 5);

// Exemplo onde o while é falso, mas será executado do do
var i = 5;
do {
  console.log("A condição (%d<5) é: %s", i, (i < 5) ? 'Verdadeira' : 'Falsa');
  console.log("i é igual a : %d", i);
  i++;
} while (i < 5);

// Sem o do, não será executado nenhuma vez pois a condição é falsa
while (i < 5) {
  console.log('Este bloco não será executado pois %d é menor que 5', i);
}
    
07.07.2017 / 15:54