# New to immediately-invoked function expression with JavaScript, curious about placement

At the end of the immediately-invoked function expression, there is an if statement that checks the screen width and loads in some resources. I'm curious if it is best practice to include this logic inside my IIFE, or outside. Both seem to function the same as far as I can tell, though I'm curious if there is preferred/best way this should be done.

Also of interest to me, inside the IIFE I have a if/if else/else statement. I'm curious if the last else, with the return in it is necessary. Does it offer any benefits or should I remove it.

Feedback is much appreciated!

Below is the code I'm working with:

(function(w){
w.loader = function(filetype, filename, mediaquery){

script = document.createElement('script'),
filetype = filetype.toLowerCase(); // normalize filetype data for use in conditions

// If file type is set to CSS, create dynamic stylesheet link element
if (filetype === 'css'){
stylesheet.type = 'text/css';
stylesheet.rel = 'stylesheet';
stylesheet.href =  (filename + '.css');
stylesheet.media = (mediaquery) ? mediaquery : 'screen'; // Use screen for media unless otherwise specified
// If a file name has been specified, append dynamic stylesheet link element
if (filename.length){
}
}
// If file type is set to JS, create dynamic script element link
else if(filetype === 'js'){
script.type = 'text/javascript';
script.src = (filename + '.js');
// If a file name has been specified, append dynamic script element
if (filename.length){
}
}
else{
return;
}

};

if(Math.max(screen.width,window.outerWidth) > 600 ){
}

}(this));

/*
if(Math.max(screen.width,window.outerWidth) > 600 ){
}
*/

-

jfriend00 is 100% right about the first part of your question.

To answer the second part of your question the return serves no purpose in the code your presented.

A better approach would actually be to throw an exception for the unhandled case. This way, if the user of this function tries to include something that the loader cannot handle they will know about it right away instead of having it fail silently.

-

In this case, it makes no difference at all. This IIFE really has no reason for even being there because all it contains is a single function. Your code does use any of the usual benefits of an IIFE (like a local and protected scope) or capturing the arguments in a closure.

So the additional code that doesn't have any local variables of its own and doesn't use the passed in argument does not benefit at all from being in the IIFE, but there is no harm in putting it in the IIFE either.

-