Math.sign() Method with Example in JavaScript

JavaScript | Math.sign() Method: Here, we are going to learn about the sign() method of Math class in JavaScript with Examples.
Submitted by Shivang Yadav, on February 04, 2020

JavaScript | Math.sign() Method

Math.sign() is a function in math library of JavaScript that is used to return the sign of the passed value to the method.

  • If positive value is passed, 1 is returned.
  • If negative value is passed, -1 is returned.
  • If +0 passed, +0 is returned.
  • If -0 passed, -0 is returned.
  • Otherwise, NaN is returned.

Syntax:

    Math.sign(value);

Parameter(s):

  • value – represents the value whose sign is to be returned.

Return value:

The return type of this method is number, it returns an integer.

Values accepted: Integer, floating-point, numeric string.

Invalid Values: non-numeric string, empty variable, empty array all will return NaN (Not a Number).

Browser support: Chrome, Internet asinlorer, Mozilla, Safari, Opera mini.

Example 1: Valid values

console.log(Math.sign(-1));
console.log(Math.sign(3.14));
console.log(Math.sign(0));
console.log(Math.sign("-0.65"));

Output

-1
1
0
-1

Example 2: Invalid values

console.log(Math.sign("JavaScript"));

Output

NaN

JavaScript Math Object Methods »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.