1//Function declarations load
2//before any code is executed
3//while
4//Function expressions load
5//only when the interpreter reaches that line of code.
6//eg.
7add(1,2);
8function add(a,b){ //this is a function declaration
9 return a+b;
10}
11sum(1,5); //this is a function expression will throw initialization error
12const sum = () => a+b; //function expression is strored in variable
1//Four ways to declare a function
2function add(a, b) {
3 return a + b;
4}
5
6var add = function(a, b) {
7 return a + b;
8}
9
10var add = (a, b) => {
11 return a + b;
12}
13
14var add = (a, b) => a + b;
1function myFunction(var1, var2) {
2 return var1 * var2;
3}
43
5How to create a function in javascriptJavascript By TechWhizKid on Apr 5 2020
6function addfunc(a, b) {
7 return a + b;
8 // standard long function
9}
10
11addfunc = (a, b) => { return a + b; }
12// cleaner faster way creating functions!
1var x = 10;
2
3function créerFonction1() {
4 var x = 20;
5 return new Function("return x;"); // ici |x| fait référence au |x| global
6}
7
8function créerFonction2() {
9 var x = 20;
10 function f() {
11 return x; // ici |x| fait référence au |x| local juste avant
12 }
13 return f;
14}
15
16var f1 = créerFonction1();
17console.log(f1()); // 10
18var f2 = créerFonction2();
19console.log(f2()); // 20
1//1st (simple function)
2function hello1() {
3 return "Hello simple function";
4}
5
6//2nd (functino expression)
7hello2 = function() {
8 return "Hello functino expression";
9}
10
11// 3rd ( IMMEDIATELY INVOKED FUNCTION EXPRESSIONS (llFE))
12hello3 = (function() {
13 return "Hello IMMEDIATELY INVOKED FUNCTION EXPRESSIONS (llFE)";
14}())
15
16//4th (arrow function)
17hello4 = (name) => { return ("Hello " + name); }
18 //OR
19hello5 = (name) => { return (`Hello new ${name}`) }
20
21document.getElementById("arrow").innerHTML = hello4("arrow function");
22
23document.write("<br>" + hello5("arrow function"));