You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何需用位运算符|0才能正确访问数组最后元素?代码解析

Why does removing the | 0 bitwise operator break access to the last element in my array intersection code?

Problem Description

I have a function that finds the intersection of two string-formatted number arrays. For the input ["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"], the expected result is 1,4,13. However, when I remove the | 0 bitwise operator from the code, the last intersecting element (13) isn't detected correctly. Here's my code:

function FindIntersection(strArr) {
  const firstArr = strArr[0].split(", ");
  const secondArr = strArr[1].split(", ");
  let newArr = [];
  let i = 0;
  let j = 0;
  while(i < firstArr.length && j < secondArr.length) {
    let a = firstArr[i] | 0;
    let b = secondArr[j] | 0;
    if(a === b) {
      newArr.push(a);
      i++;
      j++;
    } else if(a > b) {
      j++;
    } else if (b > a) {
      i++;
    }
  }
  strArr = newArr.join(",");
  return strArr;
}

Why Removing | 0 Breaks the Code

The core issue boils down to string vs. numeric comparison behavior in JavaScript. When you split the input strings, every element in firstArr and secondArr is a string (e.g., "7", "13"), not a number. JavaScript compares strings lexicographically (like dictionary order) instead of by their actual numeric value.

Let’s walk through the exact failure point without | 0:

  • When i = 3, firstArr[i] = "7"; j = 3, secondArr[j] = "13"
  • Comparing "7" > "13" as strings: The first character of "7" is "7" (ASCII code 55), while the first character of "13" is "1" (ASCII code 49). Since 55 > 49, JavaScript evaluates "7" > "13" as true.
  • This triggers the j++ branch, moving j to 4 (element "15"). Comparing "7" > "15" is still true, so j increments again to 5—exceeding secondArr.length. The loop exits before it ever checks the "13" element in firstArr.

How | 0 Fixes the Problem

The | 0 bitwise OR operator is a concise way to convert a string (or any value) to a 32-bit integer. Here’s exactly what it does for your code:

  • It takes string values like "7" or "13" and converts them to their numeric equivalents (7, 13).
  • Now, comparisons use actual numeric value instead of lexicographic order. So 7 > 13 evaluates to false, and 13 > 7 is true—which triggers the i++ branch instead.
  • This lets the loop progress correctly: i increments until it reaches "13" (converted to 13), and j is still at "13" (converted to 13). The code detects the match, adds 13 to the result array, and finishes as expected.

Bonus: Explicit Alternatives to | 0

If you prefer more readable conversions, you can use these instead:

  • parseInt(firstArr[i], 10) (explicit base-10 integer conversion)
  • Number(firstArr[i]) (converts to a number, works for floats too)
  • +firstArr[i] (unary plus operator, behaves like Number())

All of these will fix the comparison issue—| 0 is just a shorthand (albeit slightly opaque) idiom for integer conversion in JavaScript.


内容的提问来源于stack exchange,提问作者Benedikt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 07:22:58