Introduction to JavaScript Functions
Learn how to create and use functions in JavaScript to organize and reuse your code.
Introduction to JavaScript Functions
Functions are blocks of code that can be executed multiple times from different parts of your program.
Step 1: Defining a Function
In JavaScript, you can define a function using the function keyword.
function greet(name) {
console.log(`Hello, ${name}!`);
}
Step 2: Calling a Function
To call a function, you simply need to use its name followed by parentheses.
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet('John Doe');
Step 3: Working with Function Arguments
Functions can take arguments, which are values passed to the function when it is called.
function add(x, y) {
return x + y;
}
let result = add(5, 10);
console.log(result);
By following these steps, you can learn how to create and use functions in JavaScript.
Ready for more? These paid resources pick up where this lesson leaves off.
Project-based javascript video courses — the perfect paid next step after these free lessons.
Browse on Udemy →Hand-picked javascript books to master the fundamentals offline.
See on Amazon →Some links on this page are affiliate links: we may earn a commission at no extra cost to you. We only recommend tools we believe are genuinely useful.