JavaScript
// Method 1: Function Declaration
function greet(name) {
return "Hello, " + name + "!";
}
console.log("Function declaration:", greet("John"));
// Method 2: Function Expression
const greet2 = function(name) {
return "Hello, " + name + "!";
};
console.log("Function expression:", greet2("Jane"));
// Method 3: Arrow Function (ES6)
const greet3 = (name) => {
return "Hello, " + name + "!";
};
console.log("Arrow function:", greet3("Bob"));
// Method 4: Arrow Function (one-liner)
const greet4 = name => "Hello, " + name + "!";
console.log("Arrow one-liner:", greet4("Alice"));
// Method 5: Function with default parameters
function greet5(name = "Guest") {
return "Hello, " + name + "!";
}
console.log("\nWith default:");
console.log(greet5("Tom"));
console.log(greet5());
// Method 6: Function with multiple parameters
function calculate(a, b, operation = 'add') {
switch(operation) {
case 'add':
return a + b;
case 'subtract':
return a - b;
case 'multiply':
return a * b;
case 'divide':
return a / b;
default:
return "Invalid operation";
}
}
console.log("\nCalculate function:");
console.log("10 + 5 =", calculate(10, 5, 'add'));
console.log("10 - 5 =", calculate(10, 5, 'subtract'));
console.log("10 * 5 =", calculate(10, 5, 'multiply'));
console.log("10 / 5 =", calculate(10, 5, 'divide'));Output
Function declaration: Hello, John! Function expression: Hello, Jane! Arrow function: Hello, Bob! Arrow one-liner: Hello, Alice! With default: Hello, Tom! Hello, Guest! Calculate function: 10 + 5 = 15 10 - 5 = 5 10 * 5 = 50 10 / 5 = 2
This program demonstrates different ways to define functions in JavaScript.
Method 1: Function Declaration
Traditional function:
javascriptfunction functionName(parameters) { // code return value; }
Characteristics:
- Hoisted (can call before declaration)
- Has its own
thiscontext - Can be used as constructor
Method 2: Function Expression
Assign function to variable:
javascriptconst func = function(parameters) { // code };
Characteristics:
- Not hoisted
- Anonymous function assigned to variable
- Can be reassigned
Method 3: Arrow Function
ES6 syntax:
javascriptconst func = (parameters) => { // code return value; };
Method 4: Arrow Function (One-liner)
Implicit return:
javascriptconst func = parameter => expression;
Arrow Function Features:
- Shorter syntax
- No
thisbinding (uses parent scope) - Cannot be used as constructor
- Implicit return for single expressions
Method 5: Default Parameters
ES6 feature:
javascriptfunction func(param = defaultValue) { // code }
When to Use:
-
Declaration: General purpose, hoisting needed
-
Expression: When reassignment needed
-
Arrow: Modern, concise, no
thisbinding -
Default params: Optional parameters
Function vs Arrow Function:
| Feature | Function | Arrow |
|---|---|---|
this binding | Own context | Parent scope |
| Hoisting | Yes | No |
| Constructor | Yes | No |
| Arguments | Yes | No |
| Syntax | Verbose | Concise |
Best Practices:
- Use arrow functions for callbacks
- Use declarations for main functions
- Use default parameters for optional args