Datatypes in Node.js

Node.js contains various types of data types similar to JavaScript.

  • Boolean
  • Undefined
  • Null
  • String
  • Number

Loose Typing: Node.js supports loose typing, which means you don’t need to specify what type of information will be stored in a variable in advance. We use the var and let keywords in Node.js declare any type of variable. Examples are given below: 

Example: 

javascript
// Variable store number data type
let a = 35;
console.log(typeof a);

// Variable store string data type
a = "w3wiki";
console.log(typeof a);

// Variable store Boolean data type
a = true;
console.log(typeof a);

// Variable store undefined (no value) data type
a = undefined;
console.log(typeof a);

Output
number
string
boolean
undefined

Objects and Functions: Node.js objects are the same as JavaScript objects i.e. the objects are similar to variables and it contains many values which are written as name: value pairs. Name and value are separated by a colon and every pair is separated by a comma. 

Example: 

javascript
let company = {
    Name: "w3wiki",
    Address: "Noida",
    Contact: "+919876543210",
    Email: "abc@w3wiki.org"
};

// Display the object information
console.log("Information of variable company:", company);

// Display the type of variable
console.log("Type of variable company:", typeof company);

Output
Information of variable company: {
  Name: 'w3wiki',
  Address: 'Noida',
  Contact: '+919876543210',
  Email: 'abc@w3wiki.org'
}
Type of variable company: object

Functions in Node.js

Node.js functions are defined using the function keyword then the name of the function and parameters which are passed in the function. In Node.js, we don’t have to specify datatypes for the parameters and check the number of arguments received. Node.js functions follow every rule which is there while writing JavaScript functions. 

Example: 

javascript
function multiply(num1, num2) {

    // It returns the multiplication
    // of num1 and num2
    return num1 * num2;
}

// Declare variable
let x = 2;
let y = 3;

// Display the answer returned by
// multiply function
console.log("Multiplication of", x,
    "and", y, "is", multiply(x, y));

Output
Multiplication of 2 and 3 is 6

As you observe in the above example, we have created a function called “multiply” with parameters the same as JavaScript. 

String and String Functions in Node.js

In Node.js we can make a variable a string by assigning a value either by using single (”) or double (“”) quotes and it contains many functions to manipulate strings. Following is the example of defining string variables and functions in node.js.

Example: 

javascript
let x = "Welcome to w3wiki ";

let y = 'Node.js Tutorials';

let z = ['Geeks', 'for', 'Geeks'];

console.log(x);

console.log(y);

console.log("Concat Using (+) :", (x + y));

console.log("Concat Using Function :", (x.concat(y)));

console.log("Split string: ", x.split(' '));

console.log("Join string: ", z.join(', '));

console.log("Char At Index 5: ", x.charAt(5));

Output:

Welcome to w3wiki
Node.js Tutorials
Concat Using (+) : Welcome to w3wiki Node.js Tutorials
Concat Using Function : Welcome to w3wiki Node.js Tutorials
Split string:  [ 'Welcome', 'to', 'w3wiki', '' ]
Join string:  Geeks, for, Geeks
Char At Index 5:  m

Buffer in Node.js

In node.js, we have a data type called “Buffer” to store binary data and it is useful when we are reading data from files or receiving packets over the network. 

Javascript
let b = new Buffer(10000);//creates buffer
let str = " ";
b.write(str);
console.log(str.length);//Display the information
console.log(b.length); //Display the information
1
10000

Node.js Basics

Node.js is a powerful runtime environment that allows developers to build server-side applications using JavaScript. In this comprehensive guide, we’ll learn all the fundamental concepts of Node.js, its architecture, and examples.

Table of Content

  • What is Node.js?
  • Key features of Node.js
  • Setting Up Node.js
  • Core Modules
  • Datatypes in Node.js
  • Node.js console-based application
  • Node.js web-based application
  • Common Use Cases
  • Conclusion

Similar Reads

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime built on Chrome’s V8 JavaScript engine. It allows the creation of scalable Web servers without threading and networking tools using JavaScript and a collection of “modules” that handle various core functionalities. It can make console-based and web-based node.js applications....

Key features of Node.js

Non-blocking I/O: Node.js is asynchronous, enabling efficient handling of concurrent requests.Event-driven architecture: Developers can create event-driven applications using callbacks and event emitters.Extensive module ecosystem: npm (Node Package Manager) provides access to thousands of reusable packages....

Setting Up Node.js

Before coming into Node.js development, ensure you have Node.js and npm installed. Visit the official Node.js website to download the latest version. Verify the installation using:...

Core Modules

Node.js includes several core modules for essential functionality. Some commonly used ones are:...

Datatypes in Node.js

Node.js contains various types of data types similar to JavaScript....

Node.js console-based application

Make a file called console.js with the following code....

Node.js web-based application

Node.js web application contains different types of modules which is imported using require() directive and we have to create a server and write code for the read request and return response. Make a file web.js with the following code....

Common Use Cases

Building RESTful APIsReal-time applications using WebSocketsMicroservices architectureServerless functions with AWS Lambda...

Conclusion

Node.js is a versatile platform for building scalable and efficient applications. Dive deeper into its features and explore real-world use cases....