Data Types in Node.js

Data Types in Node.js

Details: Data Types in Node.js

Data is anything that can be represented as numbers or strings. Representing data in numbers is called storing it as numerical values. Storing data in strings is called textual representation. Every program needs to store some data and perform operations on that data. Data can be stored in multiple formats and of different types. The most common data types include integers, floating-point numbers, and Booleans. Other data types include strings, dates, arrays, and objects. Storing and processing data with node.js is easy with their preferred data types.

Data Types in Node.js, boolean, string, function, object, array

Node.js is an open-source runtime environment for creating network applications and connecting existing applications. Node.js uses JavaScript as its programming language; it’s also an alternative to HTML5. Unlike other JavaScript-based platforms, Node.js is built on Linux and does not run in a web browser. Developing and deploying an application with Node.js requires a Linux server running the Node.js runtime, which is where the name “Node” comes from.

Primitive Data Types in Node.js

There are 4 important datatypes in node.js

  • number
  • string
  • object
  • boolean

Special Data Types in Node.js

There are two unique datatypes in node.js

  • array
  • function

Node.js Number

Number datatype in Node.js is 64 pieces of floating point, both positive and negative. The parseInt() and parseFloat() functions are utilized to change over completely to a number, if it neglects to change over into a number, it returns NaN.

					var x = 10;
var y = 23;
var z = 1232;

Result of the above code:


Boolean: Data Types in Node.js

The Boolean datatype is a datatype that has one of two potential values, either true or false. In programming, it is utilized in logical representation or to control program structure. The boolean() function is utilized to change over any datatype to a boolean value. As indicated by the principles, false, 0, NaN, null, undefine, and void strings evaluate to false and different values assess to true.


Result of the above code:


Node.js String

The Node.js strings are sequences of unicode characters. these strings can be closed in single or double quotation marks, ‘Hello World’, “Hello World”. Javascript provide various functions to operate on string, like indexOf(), split(), substr(), length.

					var x = "Welcome to etutorialspoint";

console.log('John\'s new shirt.')
console.log("\"Hey, John!\", how are you.")

Result of the above code:

					Welcome to etutorialspoint
John's new shirt.
"Hey, John!", how are you.

Node.js Object

Object in Node.js is a very flexible and dynamic datatype. Its notation is visibly readable and compact and it’s written with curly braces{}. Node.js object is a link between the keys and values. These keys are identified as strings and the values can be anything. The object properties are written as name:value pairs, separated by commas.

					var employee = {
 name: "Soy",
 age: "40",
 department: "Software"

Result of the above code:


Node.js Array

An array is a collection of key/value matches. More than one item can be stored in a single variable, i.e., Array. So the array is utilized when there is a necessity to add more items to a single variable. We can make an array in node.js in two distinct ways, either utilizing the conventional notation or array strict syntax.

					var arr1 = new Array();
var arr2 = [];
					var arr1 = ['apple', 'banana', 'pear', 'orange'];

for (var i = 0; i < arr1.length; i++) {

Result of the above code:


Some predefined array functions available, like push(), pop(), join(), slice(), indexOf().

Node.js Function

The Node.js function is a method of putting together a piece of code and giving it a name so it can be used later on as a single line of code. This is defined with a function keyword, followed by a name.

Its parameters are listed in parentheses () when defining the function. When a statement is called in the defined function, the invoking statement will be returned as output when node.js execute the code.

					function msg(name) {
 console.log("Hello "+name);


Result of the above code:

					Hello John

In Node.js, we can also assign an anonymous function expression to a variable. Like –

					var x1 = function() {
	return true;

Node.js Buffer

Buffer is a super datatype of Node.js. We can utilize a buffer to control the binary data. The buffer holds binary data that can be changed over into different formats, we can utilize this while perusing file frameworks and while getting packets over the network.

					var b = new Buffer(10000);
var str = "                         ";
console.log( str.length );
console.log( b.length ); 

Result of the above code:


Don’t forget to share this post!

Leave a Comment

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

Related Article