Functions are one of the foundational building blocks in JavaScript, allowing us to encapsulate reusable logic and structure our code effectively.

What are functions?

Functions are blocks of code that are designed to perform a specific task. They are executed when “called” or “invoked.” Functions allow us to avoid repeating code by defining reusable logic.

Syntax for defining functions

JavaScript provides several ways to define functions. Let’s explore the most common ones:

Function declaration

A function declaration defines a function with a name that can be called later.

Get hands-on with 1400+ tech skills courses.