In Javascript, how to avoid NaN when adding arrays

Posted by Jonas on Stack Overflow See other posts from Stack Overflow or by Jonas
Published on 2009-12-11T00:31:06Z Indexed on 2010/05/22 6:30 UTC
Read the original article Hit count: 270

Filed under:
|
|

I'm trying to add the values of two arrays in javascript eg. [1,2,1] + [3,2,3,4]

The answer should be 4,4,4,4 but I'm either getting 4,4,4 or 4,4,4,NaN if I change the 1st array length to 4.

I know a 4th number needs to be in the 1st array, but i can't figure out how to tell javascript to make it 0 rather then undefined if there is no number.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about arrays