1function idk() {
2 alert('This is an alert!')
3}
4//call the function to make the function run by saying "Name of function + ()"
5idk()
1var x = myFunction(4, 3); // Function is called, return value will end up in x
2
3function myFunction(a, b) {
4 return a * b; // Function returns the product of a and b
5}
1function name(parameter1, parameter2, parameter3) {
2// what the function does
3}
4
1function myfunction() {
2 console.log("function");
3};
4
5myfunction() //Should say "function" in the console.
6
7function calculate(x, y, op) {
8 var answer;
9 if ( op = "add" ) {
10 answer = x + y;
11 };
12 else if ( op = "sub" ) {
13 answer = x - y;
14 };
15 else if ( op = "multi" ) {
16 answer = x * y;
17 };
18 else if ( op = "divide" ) {
19 answer = x / y;
20 };
21 else {
22 answer = "Error";
23 };
24 return answer;
25};
26
27console.log(calculate(15, 3, "divide")); //Should say 5 in console.
28//I hope I helped!
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