Introduction to JavaScript Functions
This article was AI-drafted and has not been reviewed yet. Spotted a mistake? Tell us.
Learn how to create and use functions in JavaScript in this beginner-friendly tutorial.
Introduction to JavaScript Functions
Functions are reusable blocks of code that perform a specific task. In this tutorial, we will cover the basics of JavaScript functions.
Creating a Function
To create a function in JavaScript, you can use the function keyword followed by the name of the function and a list of parameters in parentheses. Here is an example of how to create a function:
function greet(name) {
console.log(`Hello, ${name}!`);
}
Calling a Function
To call a function in JavaScript, you can simply use the name of the function followed by a list of arguments in parentheses. Here is an example of how to call a function:
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet('John Doe');
Function Return Values
Functions in JavaScript can return values using the return statement. Here is an example of how to return a value from a function:
function add(a, b) {
return a + b;
}
let result = add(2, 3);
console.log(result);
Conclusion
In this tutorial, we covered the basics of JavaScript functions. We learned how to create functions using the function keyword, how to call functions, and how to return values from functions.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based javascript courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for javascript books — check the publication year so the examples match current versions.
Search Amazon →