Trusted answers to developer questions

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

`Math.sign`

The ** Math.sign method** is used to find the positive or negative state of a number. This method will return:

`1`

if the number passed is a positive number.`-1`

if the number passed is a negative number`+0`

if the number passed is +0`-0`

if the number passed is -0`NaN`

on all other cases

console.log("Math.sign of 10", Math.sign(10));console.log("\n---------\nMath.sign of -10", Math.sign(-10));console.log("\n---------\nMath.sign of 0", Math.sign(0));console.log("\n---------\nMath.sign of 0", Math.sign(-0));console.log("\n---------\nMath.sign of undefined", Math.sign());console.log("\n---------\nMath.sign of null", Math.sign(null));

If the argument passed to the `sign`

function is not a number, then the value will be implicitly converted to a number type.

console.log("---------\nMath.sign of '10'", Math.sign('10'));console.log("\n---------\nMath.sign of '-10'", Math.sign('-10'));console.log("\n---------\nMath.sign of 'abc'", Math.sign('abc'));

Let’s say we have an array of elements, and we need to print each element and its positive/negative state. Take a look at the code below:

let arr = [1, 2, -3, 0, -4];for(let i = 0,l = arr.length; i < l; i++){if(Math.sign(arr[i]) < 0){console.log("Number is ", arr[i], " -> Negative")} else {console.log("Number is ", arr[i], " -> Positive")}}

RELATED TAGS

javascript

comunitycreator

CONTRIBUTOR

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring

Related Courses