GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaScript-Fundamentals/039-fn-arguments.js at main · sssreddys/JavaScript-Fundamentals Expand file tree/ Copy path039-fn-arguments.jsMore file actions 164 lines (128 loc) · 3.55 KB / Copy path039-fn-arguments.jsFile metadata and controls164 lines (128 loc) · 3.55 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 // PARAMETERS VS ARGUMENTS function greet(name) { // 'name' is a parameter console.log("Hello, " + name); } greet("Sarah"); // "Sarah" is an argument // USING PARAMETERS function greet(name) { console.log("Hello, " + name + "!"); } greet("Alice"); // Hello, Alice! greet("Bob"); // Hello, Bob! greet("Charlie"); // Hello, Charlie! // MULTIPLE PARAMETERS function calculateArea(width, height) { const area = width * height; console.log("The area is: " + area); } calculateArea(10, 5); // The area is: 50 calculateArea(7, 3); // The area is: 21 function introduce(name, age, city) { console.log(name + " is " + age + " years old and lives in " + city); } introduce("John", 25, "New York"); // John is 25 years old and lives in New York // DEFAULT PARAMETERS greet(); // Hello, undefined function greet(name = "Guest") { console.log("Hello, " + name); } greet(); // Hello, Guest greet("Sarah"); // Hello, Sarah // MORE DEFAULT PARAMETER EXAMPLES function createUser(username, role = "user", active = true) { console.log("Username: " + username); console.log("Role: " + role); console.log("Active: " + active); } createUser("john_doe"); // Username: john_doe // Role: user // Active: true createUser("admin_user", "admin", false); // Username: admin_user // Role: admin // Active: false // THE ARGUMENTS OBJECT function showArguments() { console.log(arguments); } showArguments(1, 2, 3, 4, 5); // [Arguments] { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 } // ACCESSING ARGUMENTS OBJECT function printArguments() { console.log("First argument: " + arguments[0]); console.log("Second argument: " + arguments[1]); console.log("Number of arguments: " + arguments.length); } printArguments("Hello", "World", 123); // First argument: Hello // Second argument: World // Number of arguments: 3 // PRACTICAL USE OF ARGUMENTS OBJECT function sum() { let total = 0; for (let i = 0; i < arguments.length; i++) { total += arguments[i]; } return total; } console.log(sum(1, 2, 3)); // 6 console.log(sum(10, 20, 30, 40)); // 100 console.log(sum(5)); // 5 console.log(sum(1, 2, 3, 4, 5, 6)); // 21 // ANOTHER PRACTICAL EXAMPLE function findMax() { let max = arguments[0]; for (let i = 1; i < arguments.length; i++) { if (arguments[i] > max) { max = arguments[i]; } } return max; } console.log(findMax(5, 12, 3, 9, 1)); // 12 console.log(findMax(100, 50, 200, 75)); // 200 // ARGUMENTS OBJECT LIMITATIONS function demonstrateArguments() { // This won't work: // arguments.forEach(...) // ERROR! // Convert to real array: const argsArray = Array.from(arguments); argsArray.forEach(arg => { console.log(arg); }); } demonstrateArguments("apple", "banana", "cherry"); // apple // banana // cherry // MIXING PARAMETERS AND ARGUMENTS OBJECT function greetAll(greeting) { console.log(greeting); for (let i = 1; i < arguments.length; i++) { console.log("- " + arguments[i]); } } greetAll("Welcome:", "Alice", "Bob", "Charlie"); // Welcome: // - Alice // - Bob // - Charlie // PRACTICAL EXAMPLE function log(level) { const timestamp = new Date().toLocaleTimeString(); console.log("[" + timestamp + "] " + level.toUpperCase()); for (let i = 1; i < arguments.length; i++) { console.log(" " + arguments[i]); } } log("info", "User logged in", "Session started", "Loading dashboard"); // [2:30:45 PM] INFO // User logged in // Session started // Loading dashboard log("error", "Connection failed", "Retrying..."); // [2:30:46 PM] ERROR // Connection failed // Retrying... |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |