javascript function declaration 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: 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"));