Why can a number in node be rounded when it is bitwise or calculated with 0?

  node.js, question

As shown in the figure, the execution result in REPL mode of node.js is as follows:
node.js中的结果

I feel this can be replaced.parseInt, can be avoidedparseIntTheNaNThe problem.

First, let’s look at the description of the bitwise operators:

图片描述

Note the part I selected, “operands are converted to 32-bit integers,” which is the key to your question.

As for whether it can be completely replacedparseInt, the individual from the function should be replaceable (may be thoughtless, looking forward to great god advice); However, from the perspective of software engineering, I do not recommend writing it this way. In addition to flaunting one’s skills, in eachjavascriptRunning environment inside does not have obvious performance advantages (js is usually not used for computing-intensive work), but the disadvantage is that people cannot understand it! ! And this is the biggest problem. code is first shown to people and then read to machines. for what good code is, you can look at Robert C. Martin’sclean code

Add:

On how non-numeric types are silently converted into integer parts, the followingECMAScriptSome things were said:

I can’t guarantee that the information I found this time will be accurate, but it is very close.

图片描述

When converting a string into an integer, follow the principle of the above figure. When the first step is usedToNumberAfter the principle conversion, the actual result isNaNPay attention to the part I have chosen, and the second step will be toNaNChange intozeroHere we go. My understanding is that this is why'abc' | 0 === 0Why?

The document is here:spec