JSHint - Unreachable 'break' after 'return'

what is jshint
jshint api
jshint setup
jshint maxerr
jshint mac
jshint-rhino
jshint -w117
jshint visual studio

So I have been developing a modal control for several months. It is custom coded to my clients needs but I want to release it to the generic public. I have been working to get it to minify via codekit as well as uglify.

Here is an example of the remaining warnings I am receiving.

How can I best remove theses warnings? I extended the typeof function in order to know the variations between objects and arrays.

var com = {
	load: function(template, $el, obj, append, callback){
		$.get(template, function(value){
			$.templates("tmpl", value);
			var html = $.render.tmpl(obj);
			if(append){ $el.append(html); }else{ $el.html(html); }
		}).done(function(){
			if(callback !== undefined && com.type(callback) === "function"){ callback(); }
		});
	},
	checkApiEvents: function(api){
		if(!$.isEmptyObject(api.doc)){ $.each(api.doc, function(k,v){ if(com.type(v) === "function"){ $.e.doc[k].push(v); } }); }
		if(!$.isEmptyObject(api.win)){ $.each(api.win, function(k,v){ if(com.type(v) === "function"){ $.e.win[k].push(v); } }); }
	},
	type: function(name){
		switch(name){
			case "function": return "function"; break;
			case "object": if($.isArray(name)){return "array"; }else{ return "object"; } break;
			case "string": return "string"; break;
			case "number": if(!isNaN(name)){ return "number"; }else{ return "string"; } break;
			case '': case "undefined": default: return "undefined"; break;
		}
	},
};

If you find this question looking for the error in jshint W027 - Unreachable '$' after 'return' and want to return early (for now) then an easy solution to make the jshint warning go away is to use a conditional:

function doThis(){
    if (1) return true;
    // I want to keep this but return early for now
    myVar = 0;
}

add option or comment hint to ignore 'Unreachable break after return , I would love this as well as often when debugging I'll just throw in a return statement but then jshint (run automatically) complains  JSHint, A Static Code Analysis Tool for JavaScript [ Use it online • Docs • FAQ • Install • Contribute • Blog • Twitter] JSHint is a community-driven tool that detects errors and potential problems in JavaScript code. Since JSHint is so flexible, you can easily adjust it in the environment you expect your code to execute.

You need a break to break off a loop or switch not going further down the body of the block(loop/switch). The return breaks off from the function itself. So once a return is called there no point of any statement after a return.

false positive: Unreachable 'x' after 'return' · Issue #1387 · jshint , consider the following: (ignore the symbols used; I've removed symbols from real code to help isolate and illustrate the problem space). var x;  // This is JSHint, a tool that helps to detect errors and potential. // problems in your JavaScript code. // To start, simply enter some JavaScript anywhere on this page. Your. // report will appear on the right side. // Additionally, you can toggle specific options in the Configure. function main () { return 'Hello, World!';

A return statement "exits" the function immediately so any code after it will never be run.

function hello() {
    console.log('this will run');
    return;
    console.log('this will never run');
}

Yes, you can put return statements inside if blocks, but if they're called, nothing after that return statement, or outside the if block will be run either.

function hello() {
    console.log('this will run');

    var x = true;

    if (x === true) {
        console.log('this will run');
        return;
        console.log('this will never run');
    }
    else {
        return; // this will never return
    }

    console.log('this will only run if x is false');
}

Unreachable 'if' after 'return' on JShint, I've been working on this for calculator. I received this error message Unreachable 'if' after 'return'. On JShint it says the error message is on the  …eak after return statement This options suppresses warnings about unreachable break after return. It is useful especially with auto generated code. Closes jshint#757

JSHint CLI flags, jshint a-directory/ a-directory/file-1.js: line 3, col 1, 'with' is not allowed in strict mode. a-directory/nested/file-2.js: line 3, col 3, Unreachable 'void' after 'return'. The JavaScript warning "unreachable code after return statement" occurs when using an expression after a return statement, or when using a semicolon-less return statement but including an expression directly after.

JSHint Options Reference, This can lead to unexpected warnings/errors in when upgrading between minor versions of JSHint. This option is a short hand for the most strict JSHint  Disallow Unreachable Code (no-unreachable) A number of statements unconditionally exit a block of code. Any statements after that will not be executed and may be an error. The presence of unreachable code is usually a sign of a coding error.

1151931, Warning about "braceless if" or "reliance on ASI" is best left for jshint. If we want to warn about the combination we should have a different message than this one  Command-line Interface. The JSHint CLI can be installed via npm (see the Installation page for instructions). Contents: Specifying Input · Specifying Linting Options · Special Options · Ignoring Files · Flags. Specifying Input. The jshint executable accepts file system paths as command-line arguments. If a provided path describes a file

Comments
  • Don't break after you return. Nothing executes after a return. A return is enough, the break is unreachable and unnecessary. Get rid of it.
  • Thanks Andrew, after adding a break brefore the default it cleared all the remaining warnings,