| Tutorials | JavaScript |
How to Use Functions in JavaScript
Functions in JavaScript are blocks of code that you can reuse in your program.
They help you organize your code better and avoid repeating instructions.
Declaring a Function
function greet() {
console.log("Hello, welcome to JavaScript!");
}
Calling the function:
greet(); // Displays the message in the console
Functions with Parameters
function add(a, b) {
return a + b;
}
console.log(add(5, 3)); // 8
Arrow Functions
Another way to declare functions is with arrow functions:
const multiply = (x, y) => x * y;
console.log(multiply(4, 2)); // 8
Functions help you write cleaner and more reusable code.