-4

If I enter the following code into the JavaScript console, it returns the number 1310, but why?

console.log(131+"".split(',').map(Number));

I'm adding "" to 131 to convert it to a string, then using the split function to convert that string into an array, and the map function to make sure the values in the array are Numbers.

I'm not sure where the 0 is coming from after the 131, can anyone explain this?

11
  • 2
    Check what "".split(',') produces Commented Mar 25, 2024 at 15:29
  • 2
    "I'm adding "" to 131 to convert it to a string, then using the split function to convert that string into an array" - no, you're not. You're using the .split method to convert "" into an array. It's 131+("".split(',').map(Number)); not (131+"").split(',').map(Number); (which would give [131]) or (as I'd guess you actually wanted) (131+"").split('').map(Number) -> [1, 3, 1]. Commented Mar 25, 2024 at 15:29
  • 2
    This is a good example of why using + for string concatenation/conversion isn't ideal. I'd recommend using a template string for most cases if your browser support allows it. Commented Mar 25, 2024 at 15:32
  • 2
    @DBS or just the old reliable String(num) Commented Mar 25, 2024 at 15:33
  • 1
    It isn't mapping an empty string to the number 0. The call to Number() is doing that. Commented Mar 25, 2024 at 15:38

3 Answers 3

3

The member accessor . has higher precedence than +

const obj = { foo: 41 };

console.log( 1 + obj.foo );   // 42
console.log( 1 + (obj.foo) ); // 42
console.log( (1 + obj).foo ); // undefined

Thus in 131+"".split(',').map(Number) the order of execution is (slightly abbreviated to focus on the important parts):

    131 + "".split(',') .map(Number)
//      ^ ^^^^^^^^^^^^^  ^^^^^^^^^^^ 
//      3       1             2
  1. "".split(',') - split the empty string using a single comma. This results in [ "" ] (see The confusion about the split() function of JavaScript with an empty string):

    console.log( "".split(',') ); // [ "" ]

  2. [ "" ].map(Number) - the Number function is applied to each member of the array. Since there the array after the split has a single empty string, it results in [ 0 ] because Number("") is 0 (see Number called using empty string, null, or undefined):

    console.log( Number("") );             // 0
    console.log( [ "" ].map(Number) ); // [ 0 ]

  3. 131 + [ 0 ] - adding a number and an array. This will first type coerce the array into a primitive then based on what kind of primitive it is (string or number) it will perform either numeric addition with 131 or string concatenation (coercing the other operand to a string first). Arrays convert to a string primitive by joining all members together (see Why is [1,2] + [3,4] = "1,23,4" in JavaScript?). As the array only has a single member (a zero) the result is that single member as a string.

    const result = 131 + [ 0 ];
    console.log(result, typeof result);                // 1310 string
    
    const arrayCoercion = "" + [ 0 ];
    console.log(arrayCoercion , typeof arrayCoercion); // 0 string

Therefore the code concatenates 131 and 0 together.

Sign up to request clarification or add additional context in comments.

Comments

1

To understand it better, break down of what the script you provided does:

console.log(131+"".split(',').map(Number));

Removing console.log makes following:

131+"".split(',').map(Number)

Evaluation happens firstly on 131, then on "".split(',').map(Number), essentially making it equivalent to

(131) + ("".split(',').map(Number))

(131) is just a number 131. Looking at "".split(',').map(Number), going from the leftmost:

"".split(',')

Will just return ['']

[''].map(Number)

Will essentially be equivalent to

[ Number('') ]

Since Number('') is 0, the result is [0]. Coming back:

(131) + ([0])

Is just 1310. There you go.

Comments

0

If you use 131 + "".split(",").map(Number) you are adding "".split(",").map(Number) to 131. You need to use (131 + "").split(",").map(Number) for your expected outcome.

2 Comments

Though correct, this could be expanded upon. Why is the result "1230"? Where does the "0" come from?
@3limin4t0r this is explain in VLAZ answer (stackoverflow.com/a/78220432/9947331)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.