Node js Data types

Node.js was 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.

Node.js has four core datatypes - number, boolean, string and object.

And two special data types - function and array.

Number

Number in javascript is 64 bits floating point number both positive and negative. Javascript parseInt and parseFloat are used to convert to number, if it fails to convert into a number then it returns NaN.

var x = 10;
console.log(x); 


Boolean

The value of a boolean can be either true or false. 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 evaluate to true.

if(NaN){
}else{
   console.log("false");
}


Strings

This is a sequence of unicode character. 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);


Objects

Objects are an extremely dynamic and flexible data type. Object notation is super readable and compact.


object literal syntax -

 

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

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 array is used when there is requirement to add more items in a single variable.

var arr1 = new Array();

There are some predefined array functions available, examples - push, pop, join, slice, indexOf.



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. In Node.js , functions can even have properties/attributes.

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

> msg("John");
Hello John
undefined
>

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

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

Buffer

Buffer is a super datatype of Node.js. We can use buffer when reading from file systems and when receiving packets over the network.