In JavaScript, returning a function (or an object containing functions) from another function allows us to expose methods that can be accessed and used outside the scope of the parent function. This approach is useful for encapsulating logic and creating reusable components.

Example:

var expect = function(val) {
    function toBe(num) {
        if (num === val) {
            return true;
        } else {
            throw new Error("Not Equal");
        }
    }
    function notToBe(num) {
        if (num !== val) {
            return true;
        } else {
            throw new Error("Equal");
        }
    }
    return { toBe, notToBe };
};

Explanation:

By returning the functions, we can create flexible and maintainable code that adheres to the principles of modularity and separation of concerns.