5

I've got 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 notification about Strict Warning that anonymous function doesn't always return a value

I'm interested in what that means, how can I prevent this and will it provoke any problem ? Please any ideas ? thanks :)

The Alpha
  • 143,660
  • 29
  • 287
  • 307
nanobash
  • 5,419
  • 7
  • 38
  • 56

2 Answers2

8

It's not because of the anonymous function, it's because the else with that return is redundant. You don't need it since return exits the function, if the if statement is not true then the default return will execute.

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

Edit:

As nebulae said this can be done even shorter:

(function(){
    document.getElementById("element").onclick = function(){
        return confirm("Are you sure ?");
    }
})();
elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • `return confirm("Are you sure ?");` will have the same effect, no? – nebulae Jul 27 '12 at 23:00
  • so it is warning because in else "return" is not common , yes ? – nanobash Jul 27 '12 at 23:02
  • Basically yes, `return` inside `else` is not that common, it is in `else if`, and `switch` but a single `if...else` then is redundant. – elclanrs Jul 27 '12 at 23:04
  • Understood but it still gives me the same warning , I am using Komodo IDE , maybe it is Komodo's fault ? – nanobash Jul 27 '12 at 23:13
  • I am little bit confused :), because in that file I've the same script but for other form and in that script it doesn't give me notification, but the same script below it does – nanobash Jul 27 '12 at 23:17
6

Actually the strict warning you are getting because of strict mode enabled in you script but if "use strict" is not being used in your script then I think, as you said in comment that you are using Komodo IDE and most probably you have installed Firefox Extension for Debugging which is required to support the browser-side component of JavaScript debugging.

If so then it has some settings that you can eneble or disable. To disable strict mode warnings just go to Edit Menu > Preferences > Javascript (from category) and uncheck the Enable Strict Warning Messages, that's it. But using strict mode is a good programming practice. Below is a screenshot to help you

enter image description here

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • 2
    While the names are a bit confusing, Komodo's "strict warning messages" have no connection with strict mode. They are two different things. Strict mode actually changes the semantics of your code. Komodo's strict warnings are simply extra warnings about things like this; these warnings can apply to any code, whether it uses strict mode or not. – Michael Geary Apr 13 '13 at 08:34