JavaScript Functions

What is a Function?

A function is a block of code that runs when it is called.

Functions help organize and reuse code.

Function Declaration

Loading...
Output:

Parameters

Functions can take inputs called parameters.

Loading...
Output:

Return Values

Functions can return values using the return keyword.

Loading...
Output:

Function Expression

Loading...
Output:

Arrow Functions

Arrow functions provide a shorter syntax.

Loading...
Output:

Arrow Function with Return

Loading...
Output:

Default Parameters

Loading...
Output:

Calling Functions

Functions only run when they are called.

Loading...
Output:

Full Example

Loading...
Output:

Why Functions are Important

  • Reuse code
  • Keep code organized
  • Solve problems step-by-step

Common Mistakes

  • Forgetting to call the function
  • Missing return statements
  • Confusing parameters and arguments

Practice

Create a function that takes two numbers and returns their sum.

Loading...
Output:

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic