<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<pre class="pinkBckgrnd">
// normal function declaration, function name is hello and is called using hello();
function hello() {
alert('normal function declaration');
}
hello();
</pre>
<pre class="grnBckgrnd">
// asign a function to var myFunct
var myFunct = function hello() {
alert('function assigned to a var');
}
myFunct();
</pre>
<pre class="pinkBckgrnd">
// anonymous function declaration - anon b/c function has not been given a name
// function has been assigned to the var anon and is called using anon()
var anon = function() {
alert('anon function');
};
anon();
</pre>
<pre class="grnBckgrnd">
// self-executing function - notice there is no function call
// functionName();
// after the declaration as in the prev examples
setTimeout(function() {
alert('to a setTimeout function');
}, 500);
(function() {
alert('an iffee passed');
})();
</pre>
</body>
</html>
// Code goes here
// normal function declaration, function name is hello and is called using hello();
function hello() {
alert('normal function declaration');
}
hello();
// asigning a function to a var
var myFunct = function hello() {
alert('function assigned to a var');
}
myFunct();
// anonymous function declaration - anon b/c function has not been given a name
// function has been assigned to the var anon and is called using anon()
var anon = function() {
alert('anon function');
};
anon();
// self-executing function - notice there is no function call
// functionName();
// after the declaration as in the prev examples
// when creating self-executing function use:
// (function() { }());
// not functionName, js will not recognize anything other than
// the term function
(function() {
alert('iife');
})(); // if function needs a parameter at this point put here between ()
// an example of using anon function passed to another function
// here the anon function is:
// function() {
// alert('there!');
// }
setTimeout(function() {
alert('to a setTimeout function');
}, 500);
(function() {
alert('an iffee passed');
}());
pre {
padding-top: 15px;
}
.pinkBckgrnd {
background-color: #f9d1d1;
}
.grnBckgrnd {
background-color: #d6f9d1;
}