How to use multiple answers for the same variable in just one if?

Asked

Viewed 484 times

2

It is possible to use only one if for several possible values of the same variable? For example:

Instead of writing:

var nome=""
var nome=prompt("Digite seu nome");

if (nome === null){
  alert(Você clicou em cancelar);
}

if (nome === ""){
  (seu nome não foi informado);
}

That is, some separator that allows me to use only one if for these 2 possible values of if informed within the prompt box.

  • 2

    Do you know the basics of programming logic? Search for logical operators.

  • 1

    William, do you know any logical operators? Have a look: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operadores_Logicos

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

2 answers

3

It is not possible, if you have two completely different conditions and distinct actions to be performed you will have to do them separately, it is a matter of logic and mathematics.

No use using logical operator because the conditions cannot be grouped, there will be two if. You can even user the conditional operator to avoid the if, but not to avoid two conditions which is actually what I would like.

Then using the conditional operator will only make the code more unreadable without doing what you want.

It would be useful to change the logic because if you fall into one if it makes no sense to do each other, so your code without syntax errors and without redundancy and with a little more sense doing something:

var nome = prompt("Digite seu nome");
if (nome === null) alert("Você clicou em cancelar");
else if (nome === "") alert("Seu nome não foi informado");
else alert(nome);

I put in the Github for future reference.

There is a way that I find simpler, although who has no experience may find more complicated by looking longer (Early Exit makes the flow easier to understand), and it can only be applied in one function, which is the case always in real codes, that is, so learn to do the right way (actually in real code it is all done differently from that):

function pegaNome() {
    var nome = prompt("Digite seu nome");
    if (nome === null) {
        alert("Você clicou em cancelar");
        return;
    }
    if (nome === "") {
        alert("Seu nome não foi informado");
        return;
    }
    alert(nome);
}
pegaNome();

I put in the Github for future reference.

If using the conditional operator do so to make it a little more readable (note that if it does not generate a value at the end to use in another expression or to store in variable it is operator abuse, even if it can be used), who has no experience does not realize how this can help avoid confusion in the code, but who is experienced even prefers the if:

var nome = prompt("Digite seu nome");
(nome === null)
    ? alert("Você clicou em cancelar")
    : (nome === "")
        ? alert("Seu nome não foi informado")
        : alert(nome);

I put in the Github for future reference.

It’s still weird and if you mix lines without ; where need there can go very wrong, so I always talk to use ; where it is necessary even if the language does not leave, this will avoid having to do unreadable code to work in addition to avoiding errors in corner cases. But lately we realize by the questions here that people are following "courses" that teach bad practices.

  • Jeez, you answered two of my questions at once !

1

You can use a ternary conditional operator to achieve the desired result (although you don’t think it’s the most readable):

nome === null ? alert('Você clicou em cancelar') : (nome ===  ? alert('seu nome não foi informado') : false);

Ternary Conditional Operator

The conditional operator (ternary) is the only Javascript operator that has three operands. This operator is often used as a shortcut to the if statement.

Browser other questions tagged

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