var reverse = function(x) { var temp = x.toString(); var sign = 1; const INT_MAX = Math.pow(2,31) -1; const INT_MIN = -1 * Math.pow(2,31); if (temp[0] == '-') { sign = -1; temp = temp.substr(1); } temp = temp.split("").reverse().join(""); if (temp*sign > INT_MAX || temp*sign < INT_MIN) { return 0; } return temp*sign; }; console.log(reverse("-123"));