C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Math sign() methodThe JavaScript math sign() method returns the sign of the number. It indicates whether the given number is positive, negative or zero. SyntaxThe sign() method is represented by the following syntax: Math.sign(num) Parameternum - A number. ReturnThe sign of the given number. JavaScript Math sign() method exampleHere, we will understand sign() method through various examples. Example 1Let's see an example to find out the sign of the given number. <script> document.writeln(Math.sign(12)+"<br>"); document.writeln(Math.sign(-12)+"<br>"); document.writeln(Math.sign(0)); </script> Output: 1 -1 0 Example 2Let's see some cases where sign() method returns NaN. <script> document.writeln(Math.sign(NaN)+"<br>"); document.writeln(Math.sign("string")+"<br>"); document.writeln(Math.sign()); </script> Output: NaN NaN NaN Example 3Here, you can test sign() method with your own test cases. <script> function display() { var x=document.getElementById("num").value; document.getElementById("result").innerHTML=Math.sign(x); } </script> <form> Enter a number: <input type="text" id="num"> <input type="button" onclick="display()" value="submit"> </form> <p><span id="result"></span></p>
Next TopicJavaScript Math
|