top of page

JavaScript We Need To Know Before Node.js – PART 2

Writer's picture: Anushka ShrivastavaAnushka Shrivastava

Updated: Oct 6, 2022


As Node.js uses JavaScript as its programming language, it is important to know about the basics to intermediate level of JavaScript. The official documentation of Node.js recommends having a good grasp on the following concepts:

  • Lexical Structure

  • Expressions

  • Types

  • Classes

  • Variables

  • Functions

  • This

  • Arrow functions

  • Loops

  • Scopes

  • Arrays

  • Template literals

  • Semicolons

  • Strict mode

  • ECMAScript 6, 2016, 2017

In this article, we will be dealing with the following areas of concepts –


This article is a sequel of “JavaScript We Need To Know Before Node.js PART-1”.


Other topics are covered in articles containing part-1 and part-3. You can refer to the following links for the same:



Let us try to understand and take an overview of above mentioned concepts for complete beginners to the subject.



VARIABLES


Variables can be said as named containers for a value. Value can be anything like a number we use for finding sum, or a string we use in a sentence.


Declaring a variable


There are four ways in which variables can be declared in JavaScript. They are –


1. Using var keyword

Ex – var myVar1;


2. Using let keyword

Ex – let myVar2;


3. Using const keyword

Ex – const myVar3;


4. Using nothing

Ex – myVar4


In above examples, all the variables are declared without any value. So, the variables will have undefined as their value after the execution of the code.


Initializing a variable


Once we have declared a variable, we can initialize it with a value using an assignment operator (=).

Example –


myVar1 = 28.90;
myVar2 = “Tom”;

Variables can also be initialized during declaration.

Example –


let x = “JavaScript”;
const y = 10;

To know more variables in JavaScript, you can refer to - https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Variables


LOOPS


Loops execute a block of code over and over again. Loops are useful as they are handy and they help us to run the same code a number of times, each time with same or different value as per the requirements.


For example – If we want to print a word “car” 5 times.


Instead of writing:


console.log("car");
console.log("car");
console.log("car");
console.log("car");
console.log("car");

We can write it as:


for (let i= 0; i<5; i++) {
	console.log("car");
}
	

J

avaScript supports different kind of loops. They are –

  • for loop

  • for/in loop

  • for/of loop

  • while loop

  • do while loop

In this article, we will be discussing about for loop, for/in loop and while loop


for loop


Syntax –


for ( expressions 1; expression 2; expression 3 ) { //code block

}


Where,


Expression 1: It is executed one time before the execution of the block. It is normally used to initialize a variable which will be used in the loop. Expression 1 is optional.


Expression 2: It is used to define a condition for the loop to run. If the condition holds true, loop will start over again. If it returns false, the loop will end. Expression 2 is also optional.


Expression 3: It is used to increment or decrement the value of the initial variable. Expression 3 is also optional.


For example –


for (let i = 0; i<5; i++){
    console.log(“car”);
}
    

for in loop


Syntax –


for ( key in object ) {

//code block

}


The for-in loop operates over an object or array. In terms of objects, each iteration returns a key and the key is used to access the value of the key.


For example –


const person = {fname: “John”, age = 22, subject=”Maths”};

for ( let x in person ){
    console.log(x);
}
    

while loop


Syntax –


while (condition) {

//code block

}


While loop runs through a block of code unless the given condition is true.


For example –


let i=0;
while (i<10);{
console.log(i);
i++;
}

CLASS


Classes can be defined as special functions which are template for creating objects. Their main purpose is to encapsulate data with some code, to work on that data.


Classes have syntax. There are two main components of a class syntax – class expressions and class declaration. Let us look at them in brief.


Class declaration


There are two ways of declaring a class. One way is by using class keyword with the name of the class.


Syntax –


class class_name {

//code block

}


Example –


class square {
	constructor (side) {
	this.side = side;
	}
}
	

Class Expressions


A class expression is another way of defining a class. A class can be named or unnamed via this method.


Syntax –


let variable_name = class {

//code block

}

console.log(variable_name.name) // to access the unnamed class here.


Example –



let square = class {
    constructor (side){
        this.side = side;
    }
};
    
console.log(square.name);
//Output = square

Classes are a huge topic. To know more in depth, you can refer to - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes


Let us also try to understand about the this keyword.


The this keyword refers to an object. It refers to different objects on how it is used in the code

.

An example of this is shown in the above code example of classes. To know more about it, refer to - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this


FUNCTIONS


Functions are the fundamental building blocks of a programming language. They are a set of statements which are designed to perform a particular task. It must be defined in the scope where we wish to call it.


Syntax -


function name ( parameter1, parameter2, parameter3 ){

//code to be executed

}


When JavaScript reaches at the return keyword, the function stops executing. The return value is returned to the caller.


Functions are invoked using () operator.


Example –


function productOf( a, b ){
    return a*b;
}
    
let num1=5;
let num2=10;

let result = productOf( num1 , num2 ); // calling function productOf()
console.log(result);

//Output - 50


ARROW FUNCTIONS


Arrow functions are alternative to traditional function expression. They allow us to write shorter code for functions.


Let us see code for traditional functions and arrow functions separately to understand them better.


Traditional method :


function product ( parameter1, parameter2 ) {
    return parameter1 * parameter2 ;
}
    
let result = product( 10 , 2 );
console.log ( result );
    

Using Arrow Function :


product = ( parameter1, parameter2 ) => {
return parameter1 * parameter2;
}
console.log( product );


SCOPE


Scopes can be defined as the current context of code execution, where a certain value is accessible. In short, we can say that scopes determine the accessibility of a variable or an expression.


There are mainly four types of scopes in JavaScript. They are –

  • Block Scope

  • Global Scope

  • Function Scope

  • Module Scope

Block Scope – Block scope is usually a block statement used to group some statements. These are created with curly braces that contain statements or declarations.

They only scope let and const declarations. var declarations are not scoped here.


Syntax –


{

Statement list

}


Example –



var x = 1;
{
    var x = 2;
}
console.log(x); // Output = 2 as, var declarations are not scoped

let z = 1;
{ 
    let z = 2;
    console.log(z) //Output - 2
}
console.log(z) // Output = 1;

Function Scope – These are the scopes which are only accessible within a function. A function creates a scope so that its variables cannot be accessed from outside the function.


Example –


const x = "Outside function";

function myFunction() {
	const x = "Inside function";
	console.log(x);
}
	
console.log(x);
myFunction();

Global Scope – These are the default scopes for all the code running.


Module Scope – These are the scopes for code running in module mode.



Hope you got an idea of all the concepts discussed above. JavaScript is a vast language. Practice is the key to understanding it perfectly.


Thank you.

Recent Posts

See All

Comments


Graphic Cubes

Subscribe To Get Latest Updates

Subscribe to our newsletter • Don’t miss out!

Thanks for subscribing!

Blogger
bottom of page