banner



How To Create Function In Node Js

Declaring Functions And Importing User Defined Modules in Node.js

Learn Node.js (Part 2)

Souvik Paul

Photo by Safar Safarov on Unsplash

If you haven't visited the part 1 of this series, the link is provided below.

So, let's begin with part 2.

Declaring functions

In this section, we'll discuss some ways of declaring functions in node.js.

Example 1

Here, we'll declare a function with name myfunc and will call it.

The output of the above code will be:

          Hi maverick        

Example 2

In this example, an anonymous function will be declared and assigned to a variable called myfunc.

The out of the above code will be:

          Hi there        

Example 3

Now, we'll pass a function as a parameter to another function.

Here, myfunc is passed as the parameter to the variable func in anotherFunct function. When func() will execute, it will call the myfunc function.

So, the output will be:

          printing below the contents defined in myfunc function
Hi there

Importing user defined modules

Here, we'll learn different ways to import user defined modules into a file. We'll go through four examples.

Example 1

Create two files in the same directory with names app.js and mymodule.js. The file mymodule.js will contain the following code:

And app.js will contain the following code:

From the file mymodule.js, counter function will be returned to the variable myfunc in the file app.js.

And in app.js, myfunc will store the module.exports object, which is equal to function counter of the module mymodule.js

So, when the file app.js is executed, the output will be:

          There are 4 number of items in the array        

Example 2

Again create two files in the same directory with names app.js and mymodule.js. The file mymodule.js will contain the following code:

And app.js will contain the following code:

In the above codes, module.exports of the mymodule.js module will get stored in stuff variable in app.js file. So, stuff.first in app.js file will represent module.exports.first of the mymodule.js, that is equal to counter function in mymodule.js module.

So, when the file app.js is executed, the output will be:

          There are 4 number of items in the array
The sum of both the values is 5
3.142
The sum of both the values is 13.142

Example 3

Again create two files in the same directory with names app.js and mymodule.js. The file mymodule.js will contain the following code:

And app.js will contain the following code:

Observe the difference in the methods of exporting functions in example 2 and example 3.

So, when the file app.js is executed, the output will be:

          There are 4 number of items in the array
The sum of both the values is 5
3.142
The sum of both the values is 13.142

Example 4

In this example also, we'll create two files in the same directory with names app.js and mymodule.js. The file mymodule.js will contain the following code:

And app.js will contain the following code:

This is another method of exporting functions.

So, when the file app.js is executed, the output will be:

          There are 4 number of items in the array
The sum of both the values is 5
3.142
The sum of both the values is 13.142

References

https://nodejs.org/en/docs/

Node.js documentation

How To Create Function In Node Js

Source: https://javascript.plainenglish.io/learn-node-js-part-2-4fe2ae7d5390

Posted by: eppsreck1993.blogspot.com

0 Response to "How To Create Function In Node Js"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel