const x = -123 const fn = (x) => { let res = parseInt(x.toString().split('').reverse().join('')) if(x < 0){ res = -res } return res > 2147483647 || res < -2147483648 ? 0 : res } console.log(fn(x))