How do I get the absolute value of an integer without using Math.abs?
How do I get the absolute value of a number without using math.abs? This is what I have so far: function absVal(integer) { var abs = integer * integer; return abs^2; } You can use the conditional operator and the unary negation operator : function absVal(integer) { return integer < 0 ? -integer : integer; } You can also use >> (Sign-propagating right shift) function absVal(integer) { return (integer ^ (integer >> 31)) - (integer >> 31);; } Note: this will work only with integer Since the absolute value of a number is "how far the number is from zero", a negative number can just be "flipped"