anonymous, as the name suggests, allows creating a function without any names identifier. It can be used as an argument to other functions
functions that wrap our code and create an enclosed scope around it, which help keep any state or privacy within that function
(function() {
'use strict';
// Your code here
// All function and variables are scoped to this function
})();
(function() {
alert("Hello");
})()
setTimeout(function() {
alert('Demo');
}, 3000);
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Closures</h2>
<script>
var p = 20;
function a() {
var p = 40;
b(function() {
alert(p);
});
}
function b(f) {
var p = 60;
f();
}
a();
</script>
</body>
</html>