In elementary JavaScript programming, you might've learned to write a function to sum two numbers like so:

    var sum = (num1, num2) => num1 + num2

Now if you passed two numbers like sum(2, 3) into the sum() function, you got 5. Cool, right?

But what if you or someone who has access to your function only passed in one number? Like:

    sum(2) // NaN

Really? They get "not a number" - NaN?

JavaScript is expecting two arguments, as defined by the function, and it gets only one. It tries to sum it with a second, finds none, so maybe it does the summing with emptiness, I guess, because rather than throw an error, it gives you the NaN result.

The sum of undefined

To actually see why JavaScript is giving you NaN, try running

    sum('three') // threeundefined

See? "threeundefined"?

That probably tells you that JavaScript is adding or concatenating the String "three" with its Primitive type undefined because a variable declared but not initiated/not defined (that is, not given a value) is assigned the undefined value/keyword. So, it exists (in memory, yeah), but not defined. Making sense? Now, try

    sum(undefined) // NaN

To JavaScript, the sum of undefined is NaN.

How do you prevent this from happening? How do you make sure that even if only one number is supplied to your sum function, JavaScript will run as expected and give the desired result? I mean, the sum of 1 plus nothing should be 1, right?

Restricting your function to real numeral

Just before we proceed to using reduce() and the "rest parameter" to write a proper, fail-safe sum function, let me introduce a control logic into our function that'll ensure that the user supplied a real numeral number - the function should warn the programmer/user if anything other than required is supplied.

    var sum = (num1, num2) => {
        if (typeof num1 !== 'number') return 'Kindly enter a numeric number.'
        return num1 + num2

    // Now try any of these
    sum(2) // NaN
    sum(2, 4) // 6
    sum('Three') // "Kindly enter a numeric number."
    sum(undefined) // "Kindly enter a numeric number."
    sum(NaN) // "Kindly enter a numeric number."

Now, your sum function is somewhat error-proof. But not there yet. Let's get it there, shall we?

Using reduce() and the rest parameter to sum

To ensure that our sum() function is robust enough to sum any number of arguments, even if only one is supplied, we define our expected parameters as a rest parameter, meaning we straight ahead write ...args. Then knowing how reduce() works, summing things in sequence as it loops through a list/array of these things, we run reduce on the array of "rest". Cool?

See what we got,

    var sum = (...args) => args.reduce((acc, cur) => acc + cur)

    // More error-proof sum()
    var sum = (...args) => {
        var isNotANumber = el => typeof el !== 'number'
        if (args.some(isNotANumber)) return "Please enter only numeric numbers."
        return args.reduce((acc, cur) => acc + cur)

    // Now try any of these
    sum(2) // 2
    sum(2, 4) // 6
    sum('Three') // "Kindly enter a numeric number."
    sum(undefined) // "Kindly enter a numeric number."
    sum(NaN) // NaN

And, thus, we got a nifty little "summer" helper function for us.