In JavaScript that is designed to be executed in the browser, it's considered a best practice to avoid using methods on console. Such messages are considered to be for debugging purposes and therefore not suitable to ship to the client. In general, calls using console should be stripped before being pushed to production.
console.log("Made it here.");
console.error("That shouldn't have happened.");
Rule Details
This rule disallows calls or assignments to methods of the console object.
Examples of incorrect code for this rule:
::: incorrect
/* eslint no-console: "error" */
console.log("Log a debug level message.");
console.warn("Log a warn level message.");
console.error("Log an error level message.");
console.log =foo();
:::
Examples of correct code for this rule:
::: correct
/* eslint no-console: "error" */
// custom console
Console.log("Hello world!");
:::
Options
This rule has an object option for exceptions:
"allow" has an array of strings which are allowed methods of the console object
Examples of additional correct code for this rule with a sample { "allow": ["warn", "error"] } option:
If you're using Node.js, however, console is used to output information to the user and so is not strictly used for debugging purposes. If you are developing for Node.js then you most likely do not want this rule enabled.
Another case where you might not use this rule is if you want to enforce console calls and not console overwrites. For example:
However, you might not want to manually add eslint-disable-next-line or eslint-disable-line. You can achieve the effect of only receiving errors for console calls with the no-restricted-syntax rule:
In JavaScript that is designed to be executed in the browser, it's considered a best practice to avoid using methods on console. Such messages are considered to be for debugging purposes and therefore not suitable to ship to the client. In general, calls using console should be stripped before being pushed to production.
console.log("Made it here.");
console.error("That shouldn't have happened.");
Rule Details
This rule disallows calls or assignments to methods of the console object.
Examples of incorrect code for this rule:
::: incorrect
/* eslint no-console: "error" */
console.log("Log a debug level message.");
console.warn("Log a warn level message.");
console.error("Log an error level message.");
console.log =foo();
:::
Examples of correct code for this rule:
::: correct
/* eslint no-console: "error" */
// custom console
Console.log("Hello world!");
:::
Options
This rule has an object option for exceptions:
"allow" has an array of strings which are allowed methods of the console object
Examples of additional correct code for this rule with a sample { "allow": ["warn", "error"] } option:
If you're using Node.js, however, console is used to output information to the user and so is not strictly used for debugging purposes. If you are developing for Node.js then you most likely do not want this rule enabled.
Another case where you might not use this rule is if you want to enforce console calls and not console overwrites. For example:
However, you might not want to manually add eslint-disable-next-line or eslint-disable-line. You can achieve the effect of only receiving errors for console calls with the no-restricted-syntax rule:
Unlike statically-typed languages which enforce that a function returns a specified type of value, JavaScript allows different code paths in a function to return different types of values.
A confusing aspect of JavaScript is that a function returns undefined if any of the following are true:
it does not execute a return statement before it exits
it executes return which does not specify a value explicitly
it executes return undefined
it executes return void followed by an expression (for example, a function call)
it executes return followed by any other expression which evaluates to undefined
If any code paths in a function return a value explicitly but some code path do not return a value explicitly, it might be a typing mistake, especially in a large function. In the following example:
a code path through the function returns a Boolean value true
another code path does not return a value explicitly, therefore returns undefined implicitly
functiondoSomething(condition){
if(condition){
returntrue;
}else{
return;
}
}
Rule Details
This rule requires return statements to either always or never specify values. This rule ignores function definitions where the name begins with an uppercase letter, because constructors (when invoked with the new operator) return the instantiated object implicitly if they do not return another object explicitly.
Examples of incorrect code for this rule:
::: incorrect
/*eslint consistent-return: "error"*/
functiondoSomething(condition){
if(condition){
returntrue;
}else{
return;
}
}
functiondoSomething(condition){
if(condition){
returntrue;
}
}
:::
Examples of correct code for this rule:
::: correct
/*eslint consistent-return: "error"*/
functiondoSomething(condition){
if(condition){
returntrue;
}else{
returnfalse;
}
}
functionFoo(){
if(!(thisinstanceofFoo)){
returnnewFoo();
}
this.a =0;
}
:::
Options
This rule has an object option:
"treatUndefinedAsUnspecified": false (default) always either specify values or return undefined implicitly only.
"treatUndefinedAsUnspecified": true always either specify values or return undefined explicitly or implicitly.
treatUndefinedAsUnspecified
Examples of incorrect code for this rule with the default { "treatUndefinedAsUnspecified": false } option:
If you want to allow functions to have different return behavior depending on code branching, then it is safe to disable this rule.
Source: http://eslint.org/docs/rules/