Skip to content
Related Articles
Open in App
Not now

Related Articles

Must know JavaScript topics to start with Node.js

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 24 Jan, 2023
Improve Article
Save Article

Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework and it’s not a programming language. Most people are confused and understand it’s a framework or a programming language. We often use Node.js for building back-end services like APIs like Web App or Mobile App. It’s used in production by large companies such as Paypal, Uber, Netflix, Walmart, and so on.

You must install Node.js on your computer before you can begin. Check this to install the Node.js in your windows.

Once Node.js is installed, you may begin creating your first Node.js application. Here is a straightforward illustration of how to build a web server using the HTTP module:

Example: This is the basic example of NodeJs:

Javascript




const http = require('http');
  
const hostname = '127.0.0.1';
const port = 3000;
  
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});
  
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});


Output:

 

Things to learn before Node.js:

To work with Node.js, it is helpful to have a strong understanding of the following JavaScript concepts:

1. Lexical Structure: The rules that specify how the code should be expressed are referred to as the lexical structure or syntax in JavaScript. This covers elements such as the placement of tokens (such as keywords, identifiers, and punctuation) and how they are used to construct statements and expressions.

Some of the essential elements of the JavaScript lexical structure are listed below:

  • Case sensitivity: JavaScript is case sensitive, which means that variables foo and Foo are considered to be different.
  • Whitespace: JavaScript ignores most whitespace (e.g., spaces, tabs, and newlines), so you can use it to format your code in a way that makes it easier to read.
  • Line terminators: JavaScript uses the newline character (\n) to indicate the end of a line.
  • Semicolons: JavaScript uses semicolons (;) to separate statements. You can usually omit the semicolon if the statement is on a line by itself, but it is generally a good idea to use them consistently to avoid potential issues.
  • Comments: JavaScript supports two types of comments: single-line comments (//) and multi-line comments (/* */).
  • Identifiers: Identifiers are used to name variables, functions, and other elements in the code. They can contain letters, digits, underscores (_), and dollar signs ($), and must begin with a letter, an underscore, or a dollar sign.
  • Keywords: JavaScript has a set of reserved words that cannot be used as identifiers. These include words like var, function, and if.

2. Expressions: An expression in JavaScript is a section of code that generates a value. Expressions can be used independently or as a component of a longer statement.

Expressions in JavaScript can take many distinct forms, including:

  • Literals: These fixed values are spelled out in the code as literals. Examples include booleans, strings, and integers (for instance, 42), as well as (e.g., true).
  • Variables: They are referred to be designated storage areas for values. A variable’s value might change while the program is running.
  • Operators: These unique symbols carry out operations on a single or a collection of values. Examples include comparison operators like, >, ==, and!= as well as arithmetic operators like +, -, *, and /.
  • Function calls: These expressions are ones that call functions. The expression’s value is determined by using the return value from the function after it has been called with the specified inputs.
  • Array and object literals: These expressions produce new objects or arrays.

3. Data Types: There are several forms of data that you may work with in JavaScript apps. The primary data types used in JavaScript are listed below:

  • Number: Numeric values are represented by this data type. It contains both floating-point and integer values. You don’t have to worry about discriminating between integers and floating-point numbers with JavaScript since there is just one kind of number.
  • String: A string of characters, such as words or sentences, is represented by this data type. Strings require quotation marks (‘ or “).
  • Boolean: This data type shows whether a value is true or false. It is frequently used to check for specific criteria in conditional expressions.
  • Null: This data type represents the absence of a value or a null reference. It is an intentional assignment of nothingness.
  • Undefined: This data type represents the absence of a value or an uninitialized variable. It is different from null in that it is not an intentional assignment of nothingness.
  • Object: This data type represents a collection of key-value pairs. It is used to store complex data structures, such as arrays and dictionaries.

4. Classes: A class in JavaScript serves as a guide for building objects. It specifies what characteristics and operations objects made from the class will have.

With the introduction of ECMAScript 2015, classes were added to JavaScript (also known as ES6). Before this, JavaScript lacked a native class syntax, thus programmers had to write object-oriented programming using prototypes and functions.

5. ‘this’ operator in JavaScript: The ‘this’ operator in JavaScript refers to the object that is carrying out the code at the moment. It is a method of getting into the object to access it.

The way the function is called, not the way it is defined, determines the value of this. This implies that depending on the context in which the function is invoked, the value of this may change.

6. Arrow Functions: In JavaScript, an arrow function (also known as a “fat arrow function”) is a concise way to define a function expression. It has a shorter syntax compared to a regular function expression and does not have its own this, arguments, super, or new.target.

7. Loops: Loops are used in JavaScript to repeatedly run a block of code. In JavaScript, a variety of loop types are accessible.

  • for: A code block is repeated using this loop a certain number of times.
  • for-in: This loop is used to traverse over an object’s properties.
  • for-of: The values of an iterable object are iterated through in this loop (such as an array or a string).
  • while: This loop iterates around a block of code until a certain condition is met.
  • do-while: This loop is similar to a while loop, it always executes the code block at least once, and it checks the condition thereafter.

8. Scopes: A scope in JavaScript is the portion of a program where a variable is available. In JavaScript, there are two different scope types: global scope and local scope.

  • Global scope: Variables are said to be in the global scope if they are defined outside of any function. Anywhere in the program can access them.
  • Local Scope: Variables defined inside of a function are referred to as being in the local scope. Only the function in which they were declared can access them.

9. Template Literals: In JavaScript, template literals are string literals that allow you to embed expressions within a string. They are denoted by the use of backticks (`) instead of single or double quotation marks.

10. Strict Mode: Strict mode in JavaScript allows users to choose to use a more constrained version of the language. Certain forms of code that are permitted in non-strict mode will produce errors when strict mode is activated. By removing some of JavaScript’s less dependable components, strict mode is meant to assist you in writing more safe and dependable code.

You may add the phrase “use strict” at the start of a script, function, or block of code to activate the strict mode.

The following are some of the adjustments that the stringent mode makes:

  • prevents the usage of variables that are not declared.
  • prevents the unintentional creation of global variables.
  • prevents the deletion of variables, functions, or arguments that are regarded as non-configurable.
  • prevents the null or undefined value from being changed to another one.
  • prevents using a parameter with the same name as a variable or function specified in the same function.
  • prevents the naming of variables or functions with certain JavaScript keywords.

11. ECMAScript 2015 (ES6) and beyond

ECMAScript 2015(ES6): The JavaScript language had a big update with the introduction of ECMAScript 2015 (commonly known as ES6), which added several new capabilities that greatly enhanced the language. ES6 introduced a number of important innovations, some of which are:

  • Defining function expressions succinctly is possible using arrow functions.
  • Classes: These offer a syntax for designing object-oriented programming and for generating objects.
  • With the use of template literals, you may include expressions into strings.
  • These new methods for declaring variables with block scope are let and const.
  • Code may be organized and reused using modules.
  • Other modifications to the JavaScript language have been made after the introduction of ES6, including:

The exponentiation operator (**) and the Array.prototype.includes() function were added in ECMAScript 2016 (ES7).

ECMAScript 2017 (ES8): Introduced the Object.values() and Object.entries() methods, and the async and await keywords for working with asynchronous code.
ECMAScript 2018 (ES9): Introduced the async iteration and rest/spread properties syntax.
ECMAScript 2019 (ES10): Introduced the Array.prototype.flat() and Array.prototype.flatMap() methods, and the trimStart() and trimEnd() methods for strings.

12. Modules: Node.js uses a modular design, which means that you can organize your code into smaller, reusable chunks called modules. Understanding how to create, import, and export modules is crucial for building scalable and maintainable Node.js applications.

13. HTTP: Node.js includes a built-in HTTP module that allows you to create servers and make HTTP requests. Understanding how to use this module is essential for building web servers and interacting with APIs.

14. Streams:

Node.js includes a built-in stream module that allows you to work with streaming data. Understanding how to read from and write to streams is useful for working with large amounts of data or building efficient data pipelines.

15. File system: Node.js includes a built-in file system module that allows you to read from and write to the file system. Understanding how to use this module is useful for reading and writing data to the file system, as well as interacting with the file system in other ways.

16. Debugging: Debugging Node.js applications can be challenging due to the asynchronous nature of the language. Familiarity with tools such as the Node.js debugger and console.log will be helpful in troubleshooting and identifying issues in your code.

In addition to these core concepts, it is also helpful to have a strong foundation in general JavaScript programming, including concepts such as variables, functions, objects, and arrays.

17. Asynchronous programming: Asynchronous programming in JavaScript allows for the completion of time-consuming activities without impeding the main thread of operation. This allows the application to keep running while the operation is being completed in the background, which is helpful for activities that take a long time to finish, such as making network requests or reading from a file.

Node.js is built on an event-driven architecture, which means that it is designed to handle asynchronous operations. This means that you will need to be familiar with concepts such as callbacks, promises, and async/await in order to write efficient and effective Node.js code.

Asynchronous programming may be done in JavaScript in a number of methods, including:

18. Callbacks: These are functions that are passed as arguments to another function, and they are called when the task is complete.

19. Promises: These are objects that represent the eventual completion (or failure) of an asynchronous operation. They have a then() method that is called when the operation is complete, and an optional catch() method that is called if the operation fails.

20. Async/await: These are keywords that can be used to write asynchronous code that looks synchronous. They are used in combination with async functions and Promises.

21. Timers: In JavaScript, timers are functions that allow you to execute code at a later time.

There are two types of timers: setTimeout and setInterval.

  • setTimeout: This function executes a piece of code after a specified number of milliseconds have passed.
  • setInterval: This function executes a piece of code repeatedly at a specified interval (in milliseconds).

22. Closures: A closure in JavaScript is a function that continues to have access to variables from its outer (enclosing) function even after it has returned.

23. The Event Loop: In JavaScript, the event loop is a mechanism that handles the execution of code in response to events. It is responsible for executing code that is waiting on other events to complete, such as timers or network requests.

The event loop works by continuously checking a queue of pending events and executing the code associated with those events. When there are no more pending events, the event loop waits for new events to be added to the queue.

The event loop is an important part of the JavaScript runtime and is what allows the language to be asynchronous and non-blocking.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!