What are functions?
Functions are reusable blocks of code. You define them once and call them whenever you need that logic. If you’ve writtendef in Python, you already understand the concept — JavaScript just wraps it differently.
Function declarations
The most common way to create a function:- JavaScript
- Python
function instead of def, curly braces instead of indentation, and camelCase instead of snake_case.
Return values
Functions returnundefined by default. If you want a value back, you need an explicit return statement.
return also exits the function immediately. Code after return never runs.
Function expressions
You can also create functions by assigning them to variables:multiply.
Function declarations are “hoisted” — you can call them before they appear in your code. Function expressions are not. For now, stick with function declarations. You’ll use function expressions more when you learn arrow functions next.
Functions as values
JavaScript relies much more heavily on functions as values in everyday programming patterns — but Python supports the same concept.- JavaScript
- Python
.map(), .filter(), and .forEach() all take functions as arguments. React event handlers work the same way.
Calling vs referencing
A subtle but important distinction:() you call the function. Without parentheses, you reference it. This matters when passing functions to event handlers or array methods — you pass the reference, not the result.
Common mistakes
Forgetting to return a value
Forgetting to return a value
Calling a function reference instead of passing it
Calling a function reference instead of passing it
Semicolon after function declarations
Semicolon after function declarations
What’s next?
You know how to write and call functions. JavaScript has a shorter, more modern syntax for functions called arrow functions — you’ll use them everywhere in React.Arrow functions
Write shorter, cleaner functions with the modern syntax