JavaScript Math.sign() Examples [In-Depth Tutorial]


Written By - Olorunfemi Akinlua
Advertisement

In JavaScript, the Math.sign() method is used to determine the sign of a number, i.e., whether it is positive, negative, or zero. This method is a part of the Math object, which provides a range of mathematical functions that can be used in JavaScript applications. This function returns one of three values:

  • If the number is positive, it returns 1.
  • If the number is negative, it returns -1.
  • If the number is zero, it returns 0.

In this article, we will explore how the Math.sign() method can be used to identify the sign of a number in JavaScript. We will also provide some examples to demonstrate the use of this method in different use cases.

 

Positive and Negative numbers in Mathematics

In mathematics, numbers are classified into three categories: positive, negative, and zero. Positive numbers are greater than zero, negative numbers are less than zero, and zero has no sign. For example, the numbers 5, 10, and 25 are positive, while -5, -10, and -25 are negative. The number 0 is neither positive nor negative.

In JavaScript, we can use the Math.sign() method to determine the sign of a number.

 

Using the Math.sign() method in JavaScript

The Math.sign() method takes a single argument, which is the number whose sign we want to determine. The method returns one of three possible values: 1 if the number is positive, -1 if the number is negative, and 0 if the number is zero.

The syntax for using the Math.sign() method is as follows:

Math.sign(x)

Where x is the number whose sign we want to determine.

 

Example 1: Obtaining a number signs

Let's say we want to determine the sign of the number 5. We can use the Math.sign() method to achieve this.

Advertisement
const x = 5;
const y = -6;
const z = 0;

decider(x);
decider(y);
decider(z);

function decider(num) {
    const value = Math.sign(num);

    if (value === 1) {
        console.log("Number is positive");
    } else if (value === -1) {
        console.log("Number is negative");
    } else {
        console.log("That's Zero");
    }
}

Output

Number is positive
Number is negative
That's Zero

In this example, we define the variable x, y and z to be 5, 6 and 0 respectively. We then use the decider() function to log if the number is positive or negative number. We use the Math.sign() method to determine the sign of each numbers and store the result in the variable value. Afterwards, we check using the if statement and use the console.log() method to output the appropriate value.

 

Example 2: Using ternary operator

The Math.sign() method can also be used in combination with the ternary operator to perform conditional operations. For example, let's say we want to perform different operations based on the sign of a variable z.

let z = 7;
let sign = Math.sign(z);

sign === 1
    ? console.log(`${z} is positive`)
    : console.log(`${z} is negative or zero`);

z = -9;
sign = Math.sign(z);

sign === 1
    ? console.log(`${z} is positive`)
    : console.log(`${z} is negative or zero`);

Output

7 is positive
-9 is negative or zero

In this example, we define the variable z to be 7 and use the Math.sign() method to determine its sign. We then use the ternary operator to output a different message based on the sign of z. Since z is positive, the first message is displayed: "7 is positive". We then change the value of z to -9 and repeat the process. This time, the second message is displayed since z is negative: "-9 is negative or zero".

 

Example 3: Calculating the Angular Velocity of a Spinning Object

We can make use of the Math.sign() method within numerous calculation such as calculating the angular velocity of a spinning object which is given by the formula ω = v/r, where ω is the angular velocity, v is the tangential velocity, and r is the radius of the circle. Here's an example of how you can use the Math.sign() method to calculate the angular velocity of a spinning object:

function getAngularVelocity(v, r) {
    const tangentialVelocity = Math.sign(v) * v;
    const angularVelocity = tangentialVelocity / r;
    return angularVelocity;
}

console.log(getAngularVelocity(15, 5));
console.log(getAngularVelocity(-20, 5));

Output

3
4

In this example, we define a function called getAngularVelocity() that takes two parameters: v and r, which represent the tangential velocity and radius of a spinning object. We use the Math.sign() method to determine the sign of v, and then multiply it by v to get the tangential velocity. We then calculate the angular velocity using the formula ω = v/r and return the result.

Advertisement

 

Summary

In this article, we explored how the Math.sign() method can be used to determine the sign of a number in JavaScript. We saw that the method returns 1 for positive numbers, -1 for negative numbers, and 0 for zero. We also provided some examples to demonstrate the use of this method in different scenarios.

The Math.sign() method is a useful tool for identifying the sign of a number, and can be used in many different applications. With its ability to return different values based on the sign of a number, it is a versatile method that can be used in a variety of programming situations.

 

References

Math.sign() - JavaScript | MDN (mozilla.org)

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment