What does the "do" function do in Javascript?

Asked

Viewed 299 times

12

I searched a lot and I can’t find the functionality of do in Javascript. Example:

function b(a, b) {
    do a = a[b]; while (a && 1 !== a.nodeType);
    return a
}
  • 5

    Reference for Javascript documentation by MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference

  • Thank you. Is there any in English?

  • 1

    Yes! Only change the language of the page to en-BR or access directly here.

4 answers

12


The same as in other languages, it is just a flow control. It is a marking for a loop to know where it starts. 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 it is a command.

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

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

You can live without it, but the code can get more confusing, nothing serious, but it is possible that you have to do "juggle" or use a variable of flag for the first time to be guaranteed.

The keys were omitted because the code block only has one line, so it’s a simplified form. In general it is better to avoid this style, it gets harder to read. Could do in a row with keys:

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

In a way that’s 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 the 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á

I put in the Github for future reference.

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

  • 1

    +1 for the recommendation to always use the keys. Leave the code "scientific" (or cat walking on the keyboard) for the minifiers.

10

This is simply a loop/loop do while, but only with a line of code soon 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);

6

Only by complementing the other responses, and to help in understanding:

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

:)

3

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

I have set 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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.