Introduction to Node JS

Installing NodeJS on Fedora 26

Follow this tutorial to transfer the package to our local repository:Then install nodejs with dnf as follow: Printing Hello World

Our first program is called hello.js. We are going to print the original label “Hello World” using the reserve words: console.log.

Assigning variables

We can assign variables with var or let. For security issues: let do not use the variable beyond its scope as var does. The standard since 2015 is the use of let, so throughout this tutorial we are going to use let. We can assign numbers as well as strings, then differentiate the operator of comparison such as “==” and “===”, that check the content and the type.

Constructing objects

Properties of an object can be also be code as well as its methods. In this case, we are going to define an object called student whose has a name, lastname, age and an action called say, to claim to his professor he did the homework.Creating Functions

js4There was a convention to use in ES5 the reserved word function to declare a function such suma in this case which sum two numbers a and b.In ES6, the standard for an simple arrow function defines the parameter of a function with = and the definition of the function is followed by the => structure.

Working with Loops

The use of loops is the similar as the way you use them in other programming languages as C or Python. In this case we are reusing the addOne function, previously declared to add a unit of a number. Initialize the counter i and then working with it while it is lower than 5. Whenever i is equal to 3, print the result of addOne on i.

Playing with Arrays

Starting by declaring our array, arr defines a list of elements, no matter the type of each element. One way to print each element of arr is by using an index “i” that is assigned to 0 and loop until getting the length of the array (in this case from 0 to 2), to finally print each i position of arr. Another way is by defining a variable of the array. In this case fruit is part of the arr and when it is used inside a for loop, each word that is inside arr can be printed. key is used as index of the array, and printed:

In case you need to be aware of the use of indexes of an array, you can print the items with its respectively index as it is shown in a list of fruits as follow:List of users and the forEach use 

There are two ways to declare a list of users. The first one is by declaring a list of users separated by commas and each user has its own attributes such as id and name. The other way is by assigning each user a,b, and c with its properties or attributes and then gathered together, in this case the list is called users.

To use forEach, there are another three ways to do so. Depending of the complexity of the code when you will have more lines of code, you can choose by 1) creating a function to getPeriod of the user then call it while the forEach is used. 2) Defining a function for user inside the forEach structure. 3) Use the => to apply an action on a specific object.

Filter and maps

We are defining item.id < 3 and it will filter the values of 0,1, and 2. Since we only have id 1 and 2, the length of SomeUsers is going to be 2. Map will work on each element of the array, so the length of each element of the array is going to be shown. Julita includes a blank: 7!

Callbacks

Callbacks or High order Functions are defining as parameter of another function based on the functional paradigm. In this introduction we have used them to call the elements and index of an array 🙂

Mixing the code

Now suppose we have some students enrolled in their semesters and we need to count how many students are enrolled on each semester. Here we go:

Advertisements

About Julita Inca

Ingeniero de Sistemas UNAC, Magíster en Ciencias de la Computación PUCP, OPW GNOME 2011, Miembro de la GNOME Foundation desde el 2012, Embajadora Fedora Perú desde el 2012, ganadora del scholarship of the Linux Foundation 2012, experiencia como Admin Linux en GMD y Especialista IT en IBM, con certificaciones RHCE, RHCSA, AIX 6.1, AIX 7 Administrator e ITILv3. Experiencia académica en universidades como PUCP, USIL y UNI. HPC researcher, a simple mortal, like you!
This entry was posted in Education, τεχνολογια :: Technology and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s