1
2///////
3//JavaScript Function Declarations
4///////
5
6//4th (arrow function)
7hello4 = (name) => { return ("Hello " + name); }
8 //OR
9hello5 = (name) => { return (`Hello new ${name}`) }
10
11//1st (simple function)
12function hello1() {
13 return "Hello simple function";
14}
15
16//2nd (functino expression)
17hello2 = function() {
18 return "Hello functino expression";
19}
20
21// 3rd ( IMMEDIATELY INVOKED FUNCTION EXPRESSIONS (llFE))
22hello3 = (function() {
23 return "Hello IMMEDIATELY INVOKED FUNCTION EXPRESSIONS (llFE)";
24}())
25
1// Normal Function in JavaScript
2function Welcome(){
3 console.log("Normal function");
4}
5
6// Arrow Function
7const Welcome = () => {
8 console.log("Normal function");
9}
1// Non Arrow (standard way)
2let add = function(x,y) {
3 return x + y;
4}
5console.log(add(10,20)); // 30
6
7// Arrow style
8let add = (x,y) => x + y;
9console.log(add(10,20)); // 30;
10
11// You can still encapsulate
12let add = (x, y) => { return x + y; };
1// const add = function(x,y) {
2// return x + y;
3// }
4
5// const add = (x, y) => {
6// return x + y;
7// }
8
9const add = (a, b) => a + b;
10
11
12const square = num => {
13 return num * num;
14}
15
16// const rollDie = () => {
17// return Math.floor(Math.random() * 6) + 1
18// }
19
20const rollDie = () => (
21 Math.floor(Math.random() * 6) + 1
22)
23
1//function old way
2function PrintName(name){
3 console.log("my name is ", name) ;
4}
5// Arrow function es6
6const PrintName = (name) => {
7 return console.log("my name is " , name) ;
8}
1let numbers = (x, y, z) => (x + y + z) * 2;
2console.log(numbers(3, 5, 9))
3//Expected output:34