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 :)
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 ?");
}
})();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With