Understanding the Basics of Node.js

Introduction

In this tutorial, I want to introduce you to the basics of Node.js and this tutorial is a quick refresher. Before that, you have some basic knowledge about how javascript works, what you use it for and it’s base syntax.

Overview

Javascript is a weakly typed programming language. it’s an object-oriented programming language and is also very versatile.

1] Weakly typed programming

Weakly typed programming

The weakly type means that we have no explicit type of assignment. javascript knows types like numbers or text which is called string or booleans which is true or false. but, it doesn’t force you to define which type you’re using in a variable or in a function.

you can also have a variable where you store a number which you then change to a text variable, so you can switch types dynamically. In other languages this is different. so, this gives you greater flexibility to be able to change it and do not have strict typing, it also can lead to errors.

2] Object-oriented

Object-oriented

Object-oriented simply means that data can be organized in logical objects and we’ll see these objects in this tutorial. And also, understand the difference between primitive and reference types.

3] Versatility

Versatility in nodejs

Javascript runs in the browser and this is actually where it comes from but you can also use it with Node.js to run it on your system. so, we get this broad versatility of being able to use javascript on different platforms. And we can also perform a broad variety of tasks with javascript also depending on the platform you’re running on.

You can in the browser listen to user events, re-render the DOM and on the server or on a PC, you can work with files, with databases, etc.

Core Javascript Features

1] Core Syntax

Now, you’ll understand some core language features, variables, and functions and again you should definitely dive into a core javascript resource and Node.js.

2] Variables

So, for the variables and syntax with the var keyword, you can have var name for example store some value in there,

syntax:-

var name = 'John';
var salary = 10000;
var married = true;

console.log(name);
console.log(salary);
console.log(married);

Another variable, salary which now would be a number, it’s not enclosed in quotation marks. we can also add a third one, married and that would now be a boolean, these are some core types.

> node file-name.js

Output:- John

This would be a variable and now you can use that, for example, to simply output it like this Now, run this file with Node.js by running node file-name.js, this is the command we have to write in the terminal, navigate it into that project folder.

3] Functions

We can also define a function with the function keyword UserDetails,

function UserDetails()
{
 return
} 

this is how you create a function in javascript and there, functions can return values with the return statement. they all can get input like user name, user age and the user has a hobby, you can name these arguments as these.

function UserDetails(name,salary,married)
{
 return ('Name is ' + name + ', salary is'+ salary + ',Is married '+ married)
}   

These are now local variables only available inside of the function, not outside of it.

Now, we can also console log the result of UserDetails and we call a function by adding parentheses. and now we have to pass in data we expect here as arguments, so we need to pass in three arguments.

console.log(UserDetails('Robin',50000,true)); 

let and const keyword

1] let

Now, we have a quick look at the very basics and the core terms you have to know, let’s dive into some next-gen javascript syntax.

Var is a keyword that creates a new variable and actually it’s a bit of an outdated syntax, from now on we’ll use let instead of var and let also creates a variable, the scoping behaves a bit differently.

let name = 'john';
let salary = 6000;
let isstatus = true;

the core reason for using let is that we now also have another way of creating a variable and with that. It’s0 mean a variable that never changes which actually is the case for all three values.

But, we assign a new value to name. and we never change name or status, but to be clear about our intentions in the code and we know that we’ll never change.

2] const

we can also use a new keyword available in javascript and that’s the const keyword. This creates a so-called constant and this makes clear that we never plan on changing the value of name or status.

Now, if you try to assign it to a constant variable. So this actually already shows us the idea behind const and you want to use const as often as possible to be as clear about what happens in your Node.js code. as possible and if something should never change, make it a const so that you get an error.

const name = 'john';
let salary = 6000;
let isstatus = true;  
     
name = 'Robin' // change the value

If you do accidentally change it. So, you need to revert this change.

Working with Objects, Properties & Methods

the important thing you work with or important data structure you work within javascript are objects.

Objects

You could have something like the user object and you create an object with curly braces and then in there, you have key-value pairs.

for example

 const user = { name: 'John', age: 20};
 console.log(user);

Now, this is an object and we can log user information here, if we execute the code, then we see that object being printed here.

Object with functions

Now, objects allow us to group data together, you can also not have variables in there. so, to say but you can also have functions in there. for example a Hello function and there you can simply have an anonymous arrow function or an arrow function general as a value after the colon and then in there,


 const user = { name: 'John', age: 20,
				hello() => {
				console.log('Hello' + this.name)
				}};
 console.log(user);

so, the variables or functions inside of that object, we could now use this name. And now we can use that user and call hello with the dot notation and executing it as a function,

user.hello();

output:- Hello John

The dot notation is important on this which refers to the object or on you, referring to that object which is stored in this constant here.

Understanding Arrow Functions

Other new features in javascript are arrow functions. for example, we have declared function here.

const a= 10;
const b= 20;
const c= 30;

const result = function (a, b, c)
{
   return (a+b+c);
}
console.log(result(a,b,c));

We can rewrite this function as an arrow function in Node.js. so result. The value which we assign after the equal sign is a function.

Now we could already have done that in the past with this syntax, this is another way of defining a function, with the syntax and therefore this is like a named function here.

It is a way or a different way of defining a function. but, we can now also use a new syntax where we remove the function keyword and instead we add an arrow between the argument list and the curly braces. this arrow is an equal sign and a greater than sign.

const a= 10;
const b= 20;
const c= 30;

const result = (a, b, c) =>
{
   return (a+b+c);
}

console.log(result(a,b,c));

This also creates a function, it’s a bit shorter since we save the function keyword and it runs in the same way as this function ran before.

Arrays & Array Methods

Besides objects, another crucial data structure in Node.js or in Javascript, in general, are arrays. like movies. An array define with square brackets and in that array, you can have any data you could use too. you can use mixing text, numbers, boolean and you can even store objects in there or other arrays.

For example,

const values ['John', 20, true, {}];

You can use boolean values and you can even store objects in there or other arrays,

Now here, we have an array of text though and you can use for loops to go through that with this syntax for example, with the for of loop where we store each element for each iteration in that movie variable.

const movies ['Star Wars', 'GodFather'];
for(let movie in movies)
{
    console.log(movie);
}

Output:- 
Star Wars 
GodFather

Now, if we run this, we see movies name printed in two lines because this executes two times.

the loop is interesting and it’s also interesting that in javascript, we got a lot of built-in methods we can use on arrays. So on movies on that array,

if we add a dot, then IDE suggests a lot of methods we can use on arrays in javascript and all these methods.

javascript  methods.

the elements in the array, manipulate them, get a subset of that array, whatever we need.

Often you’ll see map for example which allows you to transform an array or transform the values and map will actually return a new array.

const movies ['Star Wars', 'GodFather'];
console.log(movies.map(movie => 'Movie:' + movie));

Output:- ['Movie: Star Wars', 'Movie: GodFather' ]

Now, the map always takes a function where you define how to edit that array or how to edit the elements in the array. That function will execute on every element in the array one after another and you return the updated version of the element.

Arrays, Objects & Reference Types

Now, we are going to learn about Objects and arrays in Node.js, it called reference types. They are reference types and thus when we store an array in constant Movies. we can still edit this array without violating the restriction that the constants must not change.

const movies ['Star Wars', 'GodFather'];

so, we use another method in Node.js, the push method which allows me to add a new element to the existing array. So this will not return a new array, it will add it to the existing array.

for example,

const movies ['Star Wars', 'GodFather'];  
        ,              ,   
movies.push('Apocalypse')

console.log(movies);

Now, if we console log movies after pushing and execute code, you’ll see the output and we get no error about editing this constant.

Output:- [‘Star Wars’, ‘GodFather’, ‘Apocalypse’]

The reason for that is that reference types only store an address pointing at the place in memory. where that array store and that pointer this address has not changed by us adding a new element.

So, the thing stored in this constant is this pointer, this address and this has not changed, thus our constant value has not changed. The thing it’s pointing at has changed.

One Comment

Leave a Reply

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