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:
// 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:
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:
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:
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.
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