if with console.log condition?

Asked

Viewed 527 times

3

Good morning, I’m trying to make an if whose condition is the result of a console.log
But I have no idea how to make the comparison because I’m a beginner in Javascript, would be more or less like this??
For it always returns the true condition :(

      if(console.log(ev.target.tagName) == DIV)}
        alert("O log retornou DIV");
      } else {
        alert("O log retornou IMG");
      }
  • 1

    Why not directly check the if value(Ev.target.tagname == "DIV") ?

  • It worked @Caique, thanks and sorry for the ignorance

  • 1

    There is no reason to excuse the site and to ask questions. Every day we learn something new. I answered your question below to supplement my comment.

  • Perfect!! Vlwww

3 answers

4

Console.log gives no return.

You have to use the console.log separate from the logic of the code. Include in the logic of the code is save work for later because you will have to take and can create bugs in this step.

Forehead like this:

const tagDiv = ev.target.tagName.toLowerCase() == 'div';
console.log('É div?', tagDiv);
if(tagDiv}
    alert("O log retornou DIV");
} else {
    alert("O log retornou IMG");
}
  • 1

    Okay, I get it !! Very grateful @Sergio :DD

4


The function console.log() serves to show something in the browser console.

Make your condition without the.log console that should work, that way:

  if(ev.target.tagName == 'DIV')}
    alert("O log retornou DIV");
  } else {
    alert("O log retornou IMG");
  }
  • Oh yes, I got it, it worked!! Thank you @Diego

1

The Javascript has some forms of Display, the Console.log() is one of them.

Console.log() Write to the browser console, it is used for debug (code debugging) purposes, it takes a single parameter and it is the value to display in the console.

Summarizing it is used to display only, what you should do is compare the content you intend to display outside the console.log, keep all logic out of it.

var x =1; //Declaro a variavel x e atribuo o valor 1 a ela.
var y =2; //Declaro a variavel y e atribuo o valor 2 a ela.

if(x+y == 3){ //se a condição for igual a 3 então exibo o texto:
  console.log("Verdadeiro, a soma é igual a 3!")
}

console.log(x+y); //Exibo a soma..

  • Got it!! obg !!

Browser other questions tagged

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