最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is an array with a single number in it considered a number? - Stack Overflow

programmeradmin7浏览0评论

While working on an isNumeric function I found this edge case:

[5] is considered a number, can be used with numerical operators like +, -,/ etc and gives 5 when given to parseFloat.

Why does JavaScript convert a single value array to a number?

For example

const x = [10];
console.log(x - 5, typeof x);

While working on an isNumeric function I found this edge case:

[5] is considered a number, can be used with numerical operators like +, -,/ etc and gives 5 when given to parseFloat.

Why does JavaScript convert a single value array to a number?

For example

const x = [10];
console.log(x - 5, typeof x);

gives

5 object
Share Improve this question edited Jun 14, 2019 at 14:05 CertainPerformance 371k55 gold badges349 silver badges356 bronze badges asked Jun 14, 2019 at 11:37 ManavMManavM 3,0982 gold badges21 silver badges33 bronze badges 2
  • 3 A tiny thing to note: "can be used with numerical operators like +" - This is correct, but the result of [10]+5 wouldn't be 15, it would be "105". As CertainPerformance explains below, the array is converted to a ma-separated string before the operation is performed, and where the - operator will coerce the surrounding expressions to numbers, the + operator does not. (e.g., "10" + 5 === "105") – Tyler Roper Commented Jun 14, 2019 at 14:16
  • 7 @TylerRoper: I find it curious (and IMHO unfortunate) that "use strict" didn't clean up more of that kind of nonsense. Some JavaScript implementations manage to eke amazing levels of performance out of the language, but many aspects of the language are mind-bogglingly horrible. – supercat Commented Jun 14, 2019 at 20:39
Add a ment  | 

1 Answer 1

Reset to default 24

The - operator attempts to coerce its surrounding expressions to numbers. [5], when converted to a primitive (joining all elements by ,), evaluates to '5', which can be clearly converted to a number without issue.

See the spec:

AdditiveExpression : AdditiveExpression - MultiplicativeExpression

  1. Let lref be the result of evaluating AdditiveExpression.
  2. Let lval be GetValue(lref).
  3. ReturnIfAbrupt(lval).
  4. Let rref be the result of evaluating MultiplicativeExpression.
  5. Let rval be GetValue(rref).
  6. ReturnIfAbrupt(rval).
  7. Let lnum be ToNumber(lval).
  8. ReturnIfAbrupt(lnum).
  9. Let rnum be ToNumber(rval).
  10. ReturnIfAbrupt(rnum).
  11. Return the result of applying the subtraction operation to lnum and rnum. See the note below 12.7.5.

Where ToNumber does, in the case of an object:

  1. Let primValue be ToPrimitive(argument, hint Number).
  2. Return ToNumber(primValue).

which leads to ToPrimitive, calling toString on the array, which leads to Array.prototype.toString, which calls .join.

发布评论

评论列表(0)

  1. 暂无评论