Anonymous function does not always return a value

I have JavaScript code like this =>

(function(){ document.getElementById("element").onclick = function(){ var r = confirm("Are you sure ?"); if (r){ return true; } else { return false; } } })(); 

this script works, but just gives me a notification about a Strict warning that an anonymous function does not always return a value

I am wondering what this means, how can I prevent it and will it cause any problems? Please have any ideas? thanks:)

+6
source share
2 answers

This is not due to an anonymous function, because else with this return is redundant. You do not need it, since return exits the function, if the if not true, then return will be executed by default.

 (function(){ document.getElementById("element").onclick = function(){ var r = confirm("Are you sure ?"); if (r){ return true; } return false; } })(); 

Edit:

As the nebulae said, this can be done even shorter:

 (function(){ document.getElementById("element").onclick = function(){ return confirm("Are you sure ?"); } })(); 
+8
source

Actually, the strict warning you get due to the strict mode the script is included in you, but if "use strict" is not used in your script, then I think, as you said in the comment, that you are using the Komodo IDE and most likely you installed Firefox Extension for Debugging , which is required to support the JavaScript component on the debug browser side.

If so, it has some settings that you can clear or disable. To disable strict mode warnings , simply go to Edit> Preferences> Javascript (from the category) and uncheck the box next to β€œEnable strict warning messages . ” But using strict mode is good programming practice. Below is a screenshot that will help you

enter image description here

+6
source

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


All Articles