• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

how do I check the sign of a java script variable ?

how do I check the sign of a java script variable ?

Here is my input/output to illustrate the problem.

Example Input/output:
Input:  var temp="-2"
Output:  This is a negative number . absolute value is =2

Input:  var temp="2"
Output:  This is a positive number . absolute value is 2

Input:  var temp="0"
Output:  This is a zero . absolute value is 0

I'm worried as javascript variable are unsigned . How do I check for a negative number then like above ?
0
cofactor
Asked:
cofactor
  • 2
1 Solution
 
ansudhindraCommented:
check if it less than 0, then multiply by -1
then you will get absolute..
0
 
ansudhindraCommented:
or you can do it like
var t=parseInt("-7");
if(t<0) { t=t*-1; }

alert("absolute value is "+t);
0
 
leakim971PluritechnicianCommented:
var temp="-2"
var tempIsNegative = !(temp == Math.abs(temp)); // true or false
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now