From 940b94be552509934fb45fb2a85b947628be2c58 Mon Sep 17 00:00:00 2001 From: Harold Treen Date: Sat, 14 Nov 2015 03:01:25 -0800 Subject: [PATCH] Add rules for prohibiting unused vars/expressions --- javascript/.eslintrc | 6 +++++- javascript/README.md | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/javascript/.eslintrc b/javascript/.eslintrc index 3b76b5a..e470874 100644 --- a/javascript/.eslintrc +++ b/javascript/.eslintrc @@ -110,6 +110,10 @@ "semi": 2, // Disallow function definitions of the form 'function myFunc() {' - "func-style": [2, "expression"] + "func-style": [2, "expression"], + + // Disallow unused variables/expressions + "no-unused-vars": 2, + "no-unused-expressions": 2 } } diff --git a/javascript/README.md b/javascript/README.md index c2f0491..cecbc97 100644 --- a/javascript/README.md +++ b/javascript/README.md @@ -88,6 +88,21 @@ var length = items.length; var name = 'foo'; ```` +## Don't have unused variables/expressions +```javascript +// Bad: Many of these variables/expressions are useless +{} // object - not used +var newArray = []; // newArray - not used +['1', '2', '3', '4'].forEach(function(num, index) { // index - not used + return num++; +}); + +// Good: Delete things you no longer need +['1', '2', '3', '4'].forEach(function(num) { + return num++; +}); +``` + ##Use semi-colons There are many more ways that things can break *without* semi-colons than *with* semi-colons.