jsHint "myFunction is defined but never used"

gespinha picture gespinha · Nov 4, 2013 · Viewed 14k times · Source

I am reviewing my javascript with JsHint, and it always returns an error, saying that a function is never used. Well, this is not entirely true.

I am integrating my javascript on the project index by external link (<script src="myJs.js"></script>), and on the index I call the functions I have on the myJs.js file after the actual file link. So it all looks like this:

<script src="myJs.js"></script>
<script>
myFunction();
</script>

The reason why I call my functions on the actual document and not in the myJs.js file is because I need some PHP functions running in it, and I need to call them from my index file.

The problem here is that JsHint returns a harmful, but annoying error, saying that "myFunction is defined but never used". Is it not aware that my function can later be called within my index? How can I solve this? Should I simply ignore it?

Answer

Karl-Johan Sj&#246;gren picture Karl-Johan Sjögren · Nov 4, 2013

At the top of myJs.js you could try adding an exported declaration with your function name, something like this.

/* exported myFunction */

This should silence your warning.