Node js Data types

Node.js is a purely open-source and built on top of the Google Chrome V8 engine and its ECMA script. The most of the Node.js syntax is similar to front-end Javascript, including data types, objects, functions and methods. Just like Javascript, various primitive datatypes are available in Node.js.


Primitive Datatypes of Node.js

There are four core datatypes of node.js

  • number
  • boolean
  • string
  • object


Special data types of Node.js

There are two special datatypes of node.js

  • function
  • array


Node.js Number

The number data type in Node.js is 64 bits floating point number both positive and negative. The parseInt() and parseFloat() functions are used to convert to number, if it fails to convert into a number then it returns NaN.

var x = 10;
var y = 23;
var z = 1232;
console.log(x); 
console.log(y); 
console.log(z); 
Output of the above code:
10
23
1232


Node.js Boolean

Boolean data type is a data type that has one of two possible values, either true or false. In programming, it is used in logical representation or to control program structure. The boolean() function is used to convert any data type to a boolean value. According to the rules, false, 0, NaN, null, undefined, empty string evaluate to false and other values evaluates to true.

Example
if(NaN){
}else{
   console.log("false");
}
if(undefined){
}else{
   console.log("false");
}
if(NaN){
}else{
   console.log("false");
}
Output of the above code:
false
false
false


Node.js String

Strings in Node.js are sequences of unicode characters. Strings can be wrapped in a single or double quotation marks.
Javascript provide many functions to operate on string, like indexOf(), split(), substr(), length.

var x = "Welcome to etutorialspoint";
console.log(x);

console.log('John\'s new shirt.')
console.log("\"Hey, John!\", how are you.")
Output of the above code:
Welcome to etutorialspoint
John's new shirt.
"Hey, John!", how are you.


Node.js Object

Object is an extremely dynamic and flexible data type. Object notation is super readable and compact. Objects are written with curly braces {}. In Node.js, an object is a mapping between the keys and values. The keys are 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"
};
console.log(employee.name);
console.log(employee.age);
console.log(employee.department);
Output of the above code:
Soy
40
Software

Node.js Array

An array is a collection of key/value pairs. We can store more than one item in only one variable, i.e., Array. So the array is used when there is a requirement to add more items in a single variable. We can create an array in node.js in two different ways, either using the traditional notation or array literal syntax.

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

for (var i = 0; i < arr1.length; i++) {
    console.log(arr1[i]);
}
Output of the above code:
apple
banana
pear
orange

There are some predefined array functions available, like push(), pop(), join(), slice(), indexOf().



Node.js Function

A function is a way of wrapping up a piece of code and giving that chunk a name, so that you can utilize that piece later in just one line of code. A Node.js function is defined with the function keyword, followed by a name. Function parameters are listed inside the parentheses () in the function definition. When the defined function was invoked from a statement, Node.js will return the output of the executed code after the invoking statement.

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

msg("John");

Output of the above code:
Hello John

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

var x1 = function() {
	console.log('Hello');
	return true;
}

Node.js Buffer

Buffer is a super datatype of Node.js. We can use a buffer to manipulate the binary data. Buffer holds binary data that can be converted into other formats, we can use this when reading from file systems and when receiving packets over the network.

Example
var b = new Buffer(10000);
var str = "                         ";
b.write(str); 
console.log( str.length );
console.log( b.length ); 
Output of the above code:
25
10000


Read more articles


General Knowledge



Learn Popular Language