Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Basic Functions in JavaScript

Learn JavaScript functions: declarations, expressions, arrow functions and default parameters. Hoisting is covered simply, with explanation and output.

BeginnerJavaScript Function ProgramsExample 1 of 3
basic-functions.js
1// Method 1: Function Declaration
2function greet(name) {
3 return "Hello, " + name + "!";
4}
5
6console.log("Function declaration:", greet("John"));
7
8// Method 2: Function Expression
9const greet2 = function(name) {
10 return "Hello, " + name + "!";
11};
12
13console.log("Function expression:", greet2("Jane"));
14
15// Method 3: Arrow Function (ES6)
16const greet3 = (name) => {
17 return "Hello, " + name + "!";
18};
19
20console.log("Arrow function:", greet3("Bob"));
21
22// Method 4: Arrow Function (one-liner)
23const greet4 = name => "Hello, " + name + "!";
24
25console.log("Arrow one-liner:", greet4("Alice"));
26
27// Method 5: Function with default parameters
28function greet5(name = "Guest") {
29 return "Hello, " + name + "!";
30}
31
32console.log("\nWith default:");
33console.log(greet5("Tom"));
34console.log(greet5());
35
36// Method 6: Function with multiple parameters
37function 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}
51
52console.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. 1

    greet("John") returns "Hello, John!".

  2. 2

    The expression and arrow versions greet Jane, Bob and Alice.

  3. 3

    greet5() with no argument uses "Guest".

  4. 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.