Duplicate Variables in JavaScript

Is there a way / tool to detect duplicate variable / method names in project JavaScript files?

+3
source share
4 answers

There is no such thing as duplicate names in Javascript. You will never get an error re-declaring a name that already exists.

To avoid overwriting existing names in Javascript, good developers do at least one of the following things:

1) Carefully save your variables from the global scope, usually adding all the names needed for the application from just one or two objects with global scope.

// No 
var foo = 1;
var bar = 2;
var bas = 3;

// Yes
var MyApp = {
    foo:1,
    bar:2,
    bas:3
}

2) Make sure that the variable name has not yet been created before it is created.

// No
var MyObj = {};

// Yes
var MyObj = MyObj || {} // Use MyObj if it exists, else default to empty object.
+6
source

jsLint can help you

http://www.jslint.com/

+3

, JavaScript Lint :

http://javascriptlint.com/

0

Source: https://habr.com/ru/post/1710170/


All Articles