Basic Functions in JavaScript
Learn JavaScript functions: declarations, expressions, arrow functions and default parameters. Hoisting is covered simply, with explanation and output.
1// Method 1: Function Declaration2function greet(name) {3 return "Hello, " + name + "!";4}56console.log("Function declaration:", greet("John"));78// Method 2: Function Expression9const greet2 = function(name) {10 return "Hello, " + name + "!";11};1213console.log("Function expression:", greet2("Jane"));1415// Method 3: Arrow Function (ES6)16const greet3 = (name) => {17 return "Hello, " + name + "!";18};1920console.log("Arrow function:", greet3("Bob"));2122// Method 4: Arrow Function (one-liner)23const greet4 = name => "Hello, " + name + "!";2425console.log("Arrow one-liner:", greet4("Alice"));2627// Method 5: Function with default parameters28function greet5(name = "Guest") {29 return "Hello, " + name + "!";30}3132console.log("\nWith default:");33console.log(greet5("Tom"));34console.log(greet5());3536// Method 6: Function with multiple parameters37function calculate(a, b, operation = 'add') {38 switch(operation) {39 case 'add':40 return a + b;41 case 'subtract':42 return a - b;43 case 'multiply':44 return a * b;45 case 'divide':46 return a / b;47 default:48 return "Invalid operation";49 }50}5152console.log("\nCalculate function:");53console.log("10 + 5 =", calculate(10, 5, 'add'));54console.log("10 - 5 =", calculate(10, 5, 'subtract'));55console.log("10 * 5 =", calculate(10, 5, 'multiply'));56console.log("10 / 5 =", calculate(10, 5, 'divide'));
Sample 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
In this one
- How to declare and call a function
- The difference between declarations, expressions and arrows
- What hoisting means
- How default parameters work
How it works
- 1
greet("John") returns "Hello, John!".
- 2
The expression and arrow versions greet Jane, Bob and Alice.
- 3
greet5() with no argument uses "Guest".
- 4
calculate(10, 5, op) returns 15, 5, 50 and 2.
Edge cases
Calling an arrow before it is defined
greet3("Bob") above its const line throws a ReferenceError.
Only function declarations are hoisted with their body.
Forgetting return
A function without return gives undefined.
Add return, or use a one-line arrow without braces.
Divide by zero
calculate(10, 0, 'divide') returns Infinity.
JavaScript does not throw. Check b before dividing if needed.
What's going on
A function is a named block of code you can run again and again. You give it inputs, called parameters, and it can give back a result with return. This program greets people and builds a small calculator to show the main ways to write one.
Method 1 is a function declaration: function greet(name) { ... }. Declarations are hoisted. Hoisting means JavaScript moves them to the top before running, so you can call greet() even on a line above where it is written. This is the difference you will feel most.
Method 2 is a function expression. The function is stored in a variable: const greet2 = function(name) { ... }. Arrow functions in Methods 3 and 4 are a shorter form: const greet3 = (name) => { ... }. Neither is hoisted like a declaration. If you call them before the line that defines them, you get an error.
Method 4 shows a one-line arrow. With no curly braces, the value after => is returned automatically.
Arrow functions also handle this differently. For an ordinary function, this depends on how the function is called. In obj.method(), this is normally obj. If you save that method in a variable and call it separately, this can be different. An arrow takes this from the surrounding code instead of from the call. That is one reason people use arrows inside callbacks.
Method 5 uses a default parameter. greet5() with no name uses "Guest". Method 6 takes three inputs and uses a switch to pick an operation, with 'add' as the default.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which function type is hoisted with its body?
Pick an answer to check it.
Question 2
What does greet5() return?
Pick an answer to check it.
Questions people ask
Which style should I use?
Declarations for main named functions, arrows for short callbacks. Both are common.
What happens if I pass too few arguments?
Missing ones are undefined, unless they have a default value.
Can a function return more than one value?
Return an object or an array to send back several values.