OiO.lk Blog javascript Returning undefined and NaN
javascript

Returning undefined and NaN


When I call a function using a method, it’s returns undefined and Nan.

When it’s called counter.increment, it should return the input "init" + 1, counter.decrement should return "init" – 1 and counter.reset should return the initial value of "init". But instead for some reason, the three console.log returns undefined and the function createCounter returns NaN.

var createCounter = function(init) {
    
    return {

        increment: (init) => {
            
            console.log(init);
            return init++;

        },

        decrement: (init) => {

            console.log(init);
            return init--;

        },

        reset: (init) => {

            console.log(init);
            return init;
            
        }

    }

};


const counter = createCounter(5)
counter.increment(); // 6
counter.reset(); // 5
counter.decrement(); // 4



You need to sign in to view this answers

Exit mobile version