function declaration javascript code example
Example 1: javascript function
// variable:
var num1;
var num2;
// function:
function newFunction(num1, num2){
return num1 * num2;
}
Example 2: declare function javascript
function myFunction(var1, var2) {
return var1 * var2;
}
Example 3: javascript function declaration
//Four ways to declare a function
function add(a, b) {
return a + b;
}
var add = function(a, b) {
return a + b;
}
var add = (a, b) => {
return a + b;
}
var add = (a, b) => a + b;
Example 4: functions in javascript
function myFunction(var1, var2) {
return var1 * var2;
}
3
How to create a function in javascriptJavascript By TechWhizKid on Apr 5 2020
function addfunc(a, b) {
return a + b;
// standard long function
}
addfunc = (a, b) => { return a + b; }
// cleaner faster way creating functions!
Example 5: js function
var x = 10;
function créerFonction1() {
var x = 20;
return new Function("return x;"); // ici |x| fait référence au |x| global
}
function créerFonction2() {
var x = 20;
function f() {
return x; // ici |x| fait référence au |x| local juste avant
}
return f;
}
var f1 = créerFonction1();
console.log(f1()); // 10
var f2 = créerFonction2();
console.log(f2()); // 20
Example 6: function declaration javascript
//1st (simple function)
function hello1() {
return "Hello simple function";
}
//2nd (functino expression)
hello2 = function() {
return "Hello functino expression";
}
// 3rd ( IMMEDIATELY INVOKED FUNCTION EXPRESSIONS (llFE))
hello3 = (function() {
return "Hello IMMEDIATELY INVOKED FUNCTION EXPRESSIONS (llFE)";
}())
//4th (arrow function)
hello4 = (name) => { return ("Hello " + name); }
//OR
hello5 = (name) => { return (`Hello new ${name}`) }
document.getElementById("arrow").innerHTML = hello4("arrow function");
document.write("<br>" + hello5("arrow function"));