if with console.log condition?

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 do the comparison because I am a beginner in JavaScript, would it be more or less like this ?? Because it always returns the condition true: (

      if(console.log(ev.target.tagName) == DIV)}
        alert("O log retornou DIV");
      } else {
        alert("O log retornou IMG");
      }
    
asked by anonymous 20.09.2017 / 14:36

3 answers

4

The console.log() function is used to display something in the browser console.

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

  if(ev.target.tagName == 'DIV')}
    alert("O log retornou DIV");
  } else {
    alert("O log retornou IMG");
  }
    
20.09.2017 / 14:40
4

The console.log does not return.

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

Test 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");
}
    
20.09.2017 / 14:38
1

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

  

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

In short it's used to display only, what you should do is compare the content you want to display out of console.log , keep all the 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..
    
20.09.2017 / 14:47