0% found this document useful (0 votes)
3K views

Node - Js Is An Open Source Server en

Node.js is a JavaScript runtime environment that allows creation of scalable web servers and supports various data types like JavaScript along with loose typing, objects, functions, strings and buffers; it can make console-based and web-based applications where code using console.log() will print to the console when run from the command line.

Uploaded by

utubeonly9455
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

Node - Js Is An Open Source Server en

Node.js is a JavaScript runtime environment that allows creation of scalable web servers and supports various data types like JavaScript along with loose typing, objects, functions, strings and buffers; it can make console-based and web-based applications where code using console.log() will print to the console when run from the command line.

Uploaded by

utubeonly9455
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Node.js is a cross-platform JavaScript runtime environment.

It allows the creation


of scalable Web servers without threading and networking tools using JavaScript and
a collection of “modules” that handle various core functionalities. It can make
console-based and web-based node.js applications.

Datatypes: 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 = "GeeksforGeeks";
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 & 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: "GeeksforGeeks",
Address: "Noida",
Contact: "+919876543210",
Email: "abc@geeksforgeeks.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: 'GeeksforGeeks',
Address: 'Noida',
Contact: '+919876543210',
Email: 'abc@geeksforgeeks.org'
}
Type of variable company: object
Functions: 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 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 GeeksforGeeks ";

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));


Node.js Tutorials
Concat Using (+) : Welcome to GeeksforGeeks Node.js Tutorials
Concat Using Function : Welcome to GeeksforGeeks Node.js Tutorials
Split string: [ 'Welcome', 'to', 'GeeksforGeeks', '' ]
Join string: Geeks, for, Geeks
Char At Index 5: m
Buffer: 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 console-based application: Make a file called console.js with the following
code.

console.log('Hello this is the console-based application');

console.log('This all will be printed in console');

// The above two lines will be printed in the console.


To run this file, open the node.js command prompt and go to the folder where the
console.js file exists and write the following command. It will display content on
console.The console.log() method of the console class prints the message passed in
the method in the console.

You might also like