NodeJs Tutorials
- NodeJs
- Install NodeJs
- Why use NodeJs
- NodeJs Process Model
- create First Application
- Run NodeJs Application
- Node.js Console
- Node.Js Modules
- URL Modules
- node.js Callback
- Node.js Events
- Upload Files
- Upload single & Multiple Files
- NodeJs File System
- NodeJs Email
- Debugging NodeJs
- .ENV
- NodeJs Mysql
- Helpers
- encription and decription in nodeJs
- Query string
- Date & Time
- Express Js
- Template Engine with Express
- MVC Pattern in Node.Js
- list of NPM Module
- Middleware
- Body Parser
- Render
- Nodemon module
- Morgan module
- Flash Message in ExpressJs
- Session
- Session store in database
- Cookies
- Helmet
- Multer
- Router: How To Use Routing In Node.Js
- App.Js
- express.json() and express.urlencoded()
- REST APIs in NodeJs
- Gloabal Objects
- Submit Form Data
- How to get Post Data in Node.js
- How to Get URL Parameters in Node.js
- How to create Node Project
- How to Insert Form Data Into the MySql Table Using Node.js
- How to fetch Data from MySQL database table using Node.js
- CRUD Example
- Await and Async
- Promises
- Login Example
- Password Encription
- How to validate Form (Form Validation) in Node.Js?
- Registration & Login form usingn Node.Js & MySql?
- Forgot & Reset Password
- File Upload in Node.Js with ExpressJs
- Resize Image Before Upload using Multer Sharp
- Upload multiple file using node.js with multer module
- Upload file using node.js with multer module
- Client IP Address
- REST API Downloading File in NodeJs
- Export and Download CSV From MySQL Database
- CRUD REST API
- CRUD REST API Example 2
- Enable HTTPS using Node
- How to send EMAIL using NodeJs?
- Dynamic dependent dropdown using NodeJs?
- Autocomplete Search
- Get and Send Data From Ajax Request
- Get and Post Data using Ajax
- Passport Authentication
- Node Js Data type
- Node Js Error
- Node Js Array Function
- Node Js String Function
- Puppeter Module
NodeJs DataType
Primitive Datatypes of Node.js
number
boolean
string
object
Non-primitive datatype of nodejs
There are two non-primitive datatypes of node.js
array
function
undefined
The value undefined means that a value has not been set yet or simply does not exist.
File Name :
var x;
console.log(x);
output : undefined
var y;
console.log(y);
y = null ;
console.log(y);
output : undefined
null
typeof
To see the type of anything in JavaScript, use the typeof operator:
console.log(typeof 5);
console.log(typeof "sana");
console.log(typeof function () { var x = 10; });
output :- number
string
function
Type Comparisons and Conversions
JavaScript has both the equality operator == and the precise equality operator ===.
console.log(101 == '101');
console.log(786 === '786');
console.log(84 == 'sana');
console.log("sana" == "SANA");
console.log("sana".toUpperCase() == "SANA");
output :-
true
false
false
false
true
To check arguments to functions:
File Name :
function fine(param) {
if (param == null || param == undefined || param == '')
throw new Error("Invalid Argument");
}
function better(param) {
if (!param)
throw new Error("Invalid Argument");
}
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.
File Name :
var x = 5;
var y = 10;
var z = 07;
console.log(x);
console.log(y);
console.log(z);
var n1 = 1;
var n2 = 2;
console.log(n1 + 1);
console.log(n1 / n2);
console.log(n1 * n2);
console.log(n1 - n2);
console.log(n1 % 2);
parseInt OR parseFloat function
You can use the functions parseInt and parseFloat to convert strings to numbers.
console.log(parseInt("10"));
console.log(parseFloat("8.4"));
console.log(parseInt("84.123"));
console.log(parseFloat("20"));
Note :-
If we use these functions with not-parsable value such as string, they return the special value NaN:
console.log(parseInt("sana"));
console.log(parseFloat("mahi"));
Output :- NaN
To test the NaN you use the isNaN();
isNaN(parseInt("sana"));
Boolean :-
Boolean data type is a data type that has one of two possible values, either true or false. 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.
console.log(0 == false);
console.log("" == false);
if(null){
}else{
console.log("false");
}
if(undefined){
}else{
console.log("false");
}
if(NaN){
}else{
console.log("false");
}
output :-
true
true
false
false
false
example Boolean
var myData = true;
console.log(myData); // true
// Boolean operations (&&, ||, !) work as expected:
console.log(true && true); // true
console.log(true && false); // false
console.log(true || false); // true
console.log(false || false); // false
console.log(!true); // false
console.log(!false); // true
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 ittutorial";
console.log(x);
console.log('Sana\'s tutorial.')
console.log("\"Hey, Sana!\", how are you.")
Output of the above code:
Welcome to ittutorial
Sana's tutorial.
"Hey, Sana!", 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.
File Name :
var users = {
name: "Sana",
age: "03",
address: "Patna"
};
console.log(employee.name);
console.log(employee.age);
console.log(employee.address);
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 = ['sana', 'mahira', 'mahtab', 'NB'];
for (var i = 0; i < arr1.length; i++) {
console.log(arr1[i]);
}
Node.js Function
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("sana mahtab");
anonymous function
var x1 = function() {
console.log('Hello');
return true;
}