JavaScript Functions

A Function in any programming language, is a block of code which performs a specific task. Functions in JavaScript (or any programming language) were created for the sole purpose of reusability.

A JavaScript Function does a certain task, having a certain amount of steps inside it, can be written once and be reused multiple times without having to rewrite the steps again and again.

A function may take some input 📩, does calculations on it 👨‍💻 and returns an output 📤.

General syntax of a JavaScript Function:

function name(params) {
    // Steps or code to be executed
}

The above syntax is explained as follows:

  • The keyword function explains to JavaScript, that a function is being created.
  • The name is the JavaScript Function’s name, which will be used to execute it or call it.
  • The params is the JavaScript Function’s input values or parameters on which we perform calculations and get output.
  • Inside the curly braces { }, we write all our function’s logic or steps or calculations which we want to perform on the given input or params.

Number of Parameters in JavaScript Functions 🧮

A JavaScript Function may also take more than one params (parameters) or may also take no params too.

Syntax for more than one params:

function name(params1, params2, param3) {
    // Steps or code to be executed
}

Syntax for no params:

function name() {
    // Steps or code to be executed
}

Later on, when you will be using JavaScript to build your projects (websites, web apps, mobile apps, etc.), you will be creating and using a lot of functions. Functions are an important part in any high level programming language. So, make sure you understand it clearly 🤫.

We’ll explain more about functions in our next topics. Don’t miss any single one of them, as they will help you grasp all detailed and fun knowledge about functions.