What is the JavaScript >>> operator and how do you use it?

后端 未结 7 2069
野性不改
野性不改 2020-11-22 02:41

I was looking at code from Mozilla that add a filter method to Array and it had a line of code that confused me.

var len = this.length >>> 0;
         


        
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 03:17

    Two reasons:

    1. The result of >>> is an "integral"

    2. undefined >>> 0 = 0 (since JS will try and coerce the LFS to numeric context, this will work for "foo" >>> 0, etc. as well)

    Remember that numbers in JS have an internal-representation of double. It's just a "quick" way of basic input sanity for length.

    However, -1 >>> 0 (oops, likely not a desired length!)

提交回复
热议问题