Saved X
Google login
SignUp with Email
X

Check if number is negative javascript

theindreshverma ( Edited  Oct 01, 12:02 am )
Login

Javascript Math.sign() method checks given number is positive or negative.

<form action="javascript:void(0);">
    <input type="number" id="n" placeholder="Enter number here" /><br><br>
    <button onclick="checkNum()">Check Number</button>
</form>
<script>
function checkNum() {
    var x = document.getElementById('n').value;
    if(Math.sign(x) == -1) {
        alert('Number is negative');
    }
    else if(Math.sign(x) == 0 || Math.sign(x) == -0) {
        alert('Number is zero');
    }
    else {
        alert('Number is positive');
    }
}
</script>

Sourcecode Tags

More Sourcecodes

More Sourcecodes