Basics
Functions
Reusable blocks of code that take inputs and return a value.
1. Explanation
Functions encapsulate logic so it can be called many times. Declare with the function keyword or use arrow functions (=>). Functions can take parameters, return values, and have default parameter values. Arrow functions are shorter and do not have their own 'this'.
2. Syntax
js
function name(params) { return value; }
const name = (params) => value;3. Example
Try it yourself
Console
Click Run to see output.
4. Expected output
output
5
36
Hello, friend
Hello, Aman5. Common mistakes
- ✗Forgetting to return — the function returns undefined
- ✗Using arrow functions when you need a method-style 'this'
- ✗Mutating parameters instead of returning a new value
6. Practice questions
- Write isEven(n) that returns true/false
- Write max(a, b, c) that returns the largest
- Write a function that converts Celsius to Fahrenheit