JavaScript Functions

A JavaScript function is a reusable block of code that can accept inputs (arguments), perform some operation or task, and return a value.

Example of function:

In this above example, add is the name of the function, a and b are the arguments passed to the function and the function returns the sum of a and b when it’s called.

Code example:

Output: 3

The add function get invoke and sum up to 3 inside function, return value and replace content of paragraph div to 3

JavaScript function in seperate JS file:

you can also. write javascript function in a separate file and import in html file . lets create a index.js file as below:

index.js file


Output: 3

In above example , the script tag attribute src will import the index.js file

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top