Positive Number to Negative Number in JavaScript?

后端 未结 13 1761
误落风尘
误落风尘 2020-12-13 05:16

Basically, the reverse of abs. If I have:

if ($this.find(\'.pdxslide-activeSlide\').index() < slideNum - 1) {
  slideNum = -slideNum
}
console.log(slideNu         


        
13条回答
  •  余生分开走
    2020-12-13 06:02

    To get a negative version of a number in JavaScript you can always use the ~ bitwise operator.

    For example, if you have a = 1000 and you need to convert it to a negative, you could do the following:

    a = ~a + 1;
    

    Which would result in a being -1000.

提交回复
热议问题