Search code examples
javascriptjslintjshint

Function declarations should not be placed in blocks. Use a function expression or move the statement to the top of the outer function


I have the following code:

if (typeof console === "object" && typeof console.error === "function") {
    function e(msg) {"use strict"; console.info(msg);}
}

For which jsLint gives the following error:

Function statements should not be placed in blocks. Use a function expression or move the statement to the top of the outer function.

Why is it giving this error and what does it mean?


Solution

  • You should not be creating function inside if block. You are much better off doing:

    var e = function(){};
    
    if(typeof console === "object" && typeof console.error === "function"){
        e = function (msg){ ... };
    }