How to check if a number is negative in JS through a function

Asked

Viewed 635 times

-2

I’m still starting to learn JS, so I just want you to explain to me and give me example.

2 answers

1

function verificaNegativo(valor) {
    if (valor < 0) {
        alert("O numero é negativo");
    } else {
        alert("O numero é Positivo");
    }
}

You can use a simple function that checks if the number passed is less than 0.

  • Very good, sounds silly, but there are times when it turns white.

1

The concept of negative number is to be less than zero.

In programming, there are "conditional tests", which perform an action if true. In JS and most languages, it is done within a structure if and else.

You can do it in your code like this:

if(numero < 0)

It is worth remembering that numero is a variable, and must be defined in advance.

Browser other questions tagged

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