JavaScript – Some ways to build functions in JavaScript

There are plenty of ways to make functions in JS. However, the nicest ones are to assign the function to a variable and to call the variable. Why this is the nicest one? Well, probably because I cannot make something similar with VBA and I would love to give a parameter to a variable there. It would look simply amazing 🙂

This is just a small code, with 4 ways of defining functions in JavaScript plus some usage of the Apply and Call in JavaScript.

<!DOCTYPE HTML>
<html>
    <head>  
    </head>
	<body>
		<script type="text/javascript">
		var square_plus_five = function square_plus_five1 (n){
			return n*n+5;
		}
		console.log(square_plus_five(5));

		var square_plus_ten = function(n){
			return n*n+10;
		}
		console.log(square_plus_ten(10));

		(function(){
			console.log("Here comes the boom!");
		})();

		var square_plus_hundred = new Function("n","return n*n-100;");
		console.log(square_plus_hundred(5));

		var numbers = [1,23,5,6];
		console.log(Math.max.apply({},numbers));
		console.log(Math.min.apply(null,numbers));
		console.log(Math.min.call(null,4,5,6,2));
		console.log(Math.max.call(null,4,5,6,2));

		</script>
    </body>
</html>

console

 

Not a lot of code, but Christmas is coming 🙂 🙂 And you may find it in GitHub here.