Reusing Code With Functions in JavaScript
JavaScript·2 min read·Jan 1, 2025
In JavaScript, a function is a reusable code structure that allows you to encapsulate a set of related statements in order to perform a specific task.
Functions allow you to easily reuse and execute these statements multiple times from various parts of a script, without having to manually repeat them.
Note that executing, calling, running, and invoking a function essentially mean the same thing and are often used interchangeably.
Declaring and executing functions
In JavaScript, functions are declared using the function keyword:
function functionName() { statements}Where:
-
functionNameis the name of the function. -
()are delimiters used to list function parameters. -
{}are delimiters used to encapsulate the function's statements, also called the body.
Unlock the Build CLI Apps in JavaScript & Node.js module
Learn how to build, integrate, and safeguard scalable CLI tools, scripts, and apps with JavaScript, Node.js, npm, and Git.
You get immediate access to:
- 45 focused lessons across JavaScript, Node.js, npm, and Git
- 17 real-world projects with commented solutions
- Ongoing updates to this bundle
- Lifetime access to this bundle