The JavaScript SIGN function is one of the JavaScript Math function which is used to find the Sign of a given expression. For example Positive, Negative or Zero. In this article we will show you, How to use SIGN() function in JavaScript Programming language with example.
Syntax of a JavaScript SIGN Function
The basic syntax of the SIGN Function in JavaScript Programming language is as shown below:
1 |
Math.sign(number); |
Number: It can be a number or a valid numerical expression.
- If the number argument is positive or negative Zero, SIGN() function will return Zero as output.
- If the number argument is Negative Number, SIGN() function will return Negative One as output.
- If the number argument is positive Number, SIGN() function will return Positive One as output.
- If the number argument is not a number, SIGN() function will return NaN.
- If the number argument is Null, SIGN() function will convert the Null value to Zero.
JavaScript SIGN Function Example
The JavaScript SIGN Function is used to find the Sign of a given expression. In this example, We are going to check the same with different data types and display the output
JAVASCRIPT CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<!DOCTYPE html> <html> <head> <title> JavaScript SIGN() Function </title> </head> <body> <h1> JavaScript SIGN Function </h1> <p id = "Pos"></p> <p id = "Neg"></p> <p id = "Dec"></p> <p id = "Neg_Dec"></p> <p id = "Str"></p> <p id = "Exp"></p> <p id = "Null"></p> <p id = "Multi"></p> <script> document.getElementById("Pos").innerHTML = Math.sign(0); document.getElementById("Neg").innerHTML = Math.sign(-0); document.getElementById("Dec").innerHTML = Math.sign(10.45); document.getElementById("Neg_Dec").innerHTML = Math.sign(-6.45); document.getElementById("Str").innerHTML = Math.sign("JavaScript"); document.getElementById("Null").innerHTML = Math.sign(null); document.getElementById("Multi").innerHTML = Math.sign(2 + 5 - 5); </script> </body> </html> |
OUTPUT
ANALYSIS
- First, We used the SIGN() Function with both the Positive and negative Zeros.
- Next, We used the SIGN() Function with both the Positive integer and negative values as arguments.
- Next, We tried SIGN() Function with String argument. As we said before, this will return NaN (Not a Number) as output
- Next, We tried SIGN() Function with Null argument and it return Zero.
- In the Last statement, We used the SIGN() Function with multiple values.
Thank You for Visiting Our Blog
Share your Feedback, or Code!!