|
| 1 | +function myInfo() { |
| 2 | + // function body |
| 3 | + console.log("Hi my name is Isfhan Ahmed"); |
| 4 | + console.log("I am from Pakistan"); |
| 5 | + console.log("I am a Software engineer"); |
| 6 | + console.log("-----------------------------"); |
| 7 | +} |
| 8 | +myInfo(); // invoke function |
| 9 | +myInfo(); |
| 10 | +myInfo(); |
| 11 | +myInfo(); |
| 12 | +// Function with required parameters |
| 13 | +function myInfoWithParameter(name, country, job) { |
| 14 | + // function body |
| 15 | + console.log("Hi my name is", name); // Argument pass log |
| 16 | + console.log("I am from ".concat(country)); // template string |
| 17 | + console.log("I am a " + job); // concatenation |
| 18 | + console.log("-----------------------------"); |
| 19 | +} |
| 20 | +myInfoWithParameter("Aisha", "Pakistan", "Teacher"); |
| 21 | +myInfoWithParameter("Areesha", "Pakistan", "Teacher and Programmer"); |
| 22 | +myInfoWithParameter("Muneeb", "Pakistan", "Programmer"); |
| 23 | +myInfoWithParameter("Sunny", "Pakistan", "Typescript Programmer"); |
| 24 | +// Function with Default Parameters |
| 25 | +function myInfoWithDefaultParameter(name, country, job) { |
| 26 | + if (job === void 0) { job = "Software Engineer"; } |
| 27 | + // function body |
| 28 | + console.log("Hi my name is", name); // Argument pass log |
| 29 | + console.log("I am from ".concat(country)); // template string |
| 30 | + console.log("I am a " + job); // concatenation |
| 31 | + console.log("-----------------------------"); |
| 32 | +} |
| 33 | +myInfoWithDefaultParameter("zubaida", "Pakistan"); |
| 34 | +myInfoWithDefaultParameter("usman", "Pakistan"); |
| 35 | +myInfoWithDefaultParameter("Aisha", "Pakistan", "Teacher"); |
| 36 | +// Function with Optional Parameters |
| 37 | +function myInfoWithOptionalParameter(name, country, job) { |
| 38 | + // function body |
| 39 | + console.log("Hi my name is", name); // Argument pass log |
| 40 | + console.log("I am from ".concat(country)); // template string |
| 41 | + if (job) { |
| 42 | + console.log("I am a " + job); // concatenation |
| 43 | + } |
| 44 | + console.log("-----------------------------"); |
| 45 | +} |
| 46 | +myInfoWithOptionalParameter("Noor ul huda", "Pakistan"); |
| 47 | +// Function with Return Type |
| 48 | +function myFunctionWithReturnType(n1, n2) { |
| 49 | + // return n1 + n2; |
| 50 | + return n1 + n2; |
| 51 | +} |
| 52 | +var result = myFunctionWithReturnType(10, 20); |
| 53 | +console.log(result); |
| 54 | +console.log(myFunctionWithReturnType(50, 20)); |
| 55 | +// Function that hoisted |
| 56 | +logDate(); |
| 57 | +function logDate() { |
| 58 | + var date = new Date(); |
| 59 | + console.log(date); |
| 60 | +} |
| 61 | +// function expression |
| 62 | +var myInfoWithFunctionExpression = function () { |
| 63 | + // function body |
| 64 | + console.log("Hi my name is Isfhan Ahmed"); |
| 65 | + console.log("I am from Pakistan"); |
| 66 | +}; |
| 67 | +myInfoWithFunctionExpression(); |
| 68 | +// Self Invoking Function |
| 69 | +(function () { |
| 70 | + // function body |
| 71 | + console.log("Hi I am Isfhan from self invoking function"); |
| 72 | +})(); |
| 73 | +// Arrow Function |
| 74 | +var myMultiplicationWithArrowFunction = function (num1, num2) { return num1 * num2; }; |
| 75 | +var result2 = myMultiplicationWithArrowFunction(10, 20); |
| 76 | +console.log(result2); |
| 77 | +// Before Arrow Function expression |
| 78 | +var beforeArrowFunction = function (num1, num2) { |
| 79 | + return num1 - num2; |
| 80 | +}; |
| 81 | +console.log(beforeArrowFunction(123, 23)); |
| 82 | +// Arrow Function with Rest Parameters |
| 83 | +var sumAllNumbers = function (message) { |
| 84 | + var numbers = []; |
| 85 | + for (var _i = 1; _i < arguments.length; _i++) { |
| 86 | + numbers[_i - 1] = arguments[_i]; |
| 87 | + } |
| 88 | + console.log(numbers); |
| 89 | + console.log(typeof numbers); |
| 90 | + console.log(message); |
| 91 | + var total = 0; |
| 92 | + for (var _a = 0, numbers_1 = numbers; _a < numbers_1.length; _a++) { |
| 93 | + var number = numbers_1[_a]; |
| 94 | + total += number; // total = total + number |
| 95 | + } |
| 96 | + return total; |
| 97 | +}; |
| 98 | +var result3 = sumAllNumbers("hello i am rest parameters function", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 99 | +console.log(result3); |
0 commit comments