NodeJS Tutorial :: Part-1

Install nodejs

Download nodejs LTS version from here

After installing it, open CMD and check nodejs version by following command:
node --version

If showing version, then nodejs has been installed successfully

Install IDE

We will install Visual Studio Code as IDE because this is very light, more friendly and support most of the plugins for nice development experience.

Download VSC from here

While install, check all of the check-boxes

First Program

Create a new folder. Give it any name

open the blank folder with visual studio

Open the terminal. Go to Terminal > New Terminal
Hit the following command:

npm init -y

package.json will be generated

create a new flle name hello.js

Write following code:

console.log('Hello world')

Now hit following command to run the program:
node hello.js

Output: Hello world

Variable

Variables are used for storing data.

For example, var name=”Hasan”; var number=10;

Here, name and number are variable

Using let and const (ES6)

Before 2015, using the var keyword was the only way to declare a JavaScript variable.

JavaScript (ES6) allows the use of the const keyword to define a variable that cannot be reassigned, and the let keyword to define a variable with restricted scope.

Variable writing rules

  1. All variable names must begin with a letter of the alphabet or an. underscore( _ )
    For example: myVariable, _number
  2. One phrase variable should be small and two phrase variable name should be camelCase
  3. One phrase variable example: name
    Two phrases variable example: firstName
  4. Variable name should not be start with number.
    For example: 2username  can not be used
  5. After the first initial letter, variable name can also contain letters and numbers.
    For example: userName1, userName2
  6. Uppercase characters are different from lowercase characters.
    For example: a and A are different variables
  7. You cannot use JavaScript reserved keywords as a variable name. For example: let. var, const etc
  8. Space or special characters are not allowed in variables.
    For example: first name, first#name, first@name

User Input

install this package:

nmm i prompt-sync

write following code:

const prompt=require('prompt-sync')();

let name=prompt('Enter your name: ')
console.log(name);

Hit following command:

node hello.js

Output: Enter your name: Salman
Salman

Commenting

Use // to comment a line or multiple line

const prompt=require('prompt-sync')();

//input your name
let name=prompt('Enter your name: ')
console.log(name);

Identifiers (let, var, const)

let and const both used in JS es6 version
let: used in local scope (also you can use it globally)
var: used in global scope (also you can use it locally)
const: used only for assigning constant variables. Constant means the variable value will not be changed or assigned.

Datatypes

There are 6 types of datatype in JavaScript

  1. Number
  2. String
  3. Boolean
  4. BigInt
  5. Symbol
  6. Undefined

For example,
var number= 10; // Number
var name= “Salman”; // String
var obj = {firstName:”Salman”, lastName:”Srabon”};    // Object

Checking Datatypes

let name="Rahim";
console.log(typeof(name))
Output: string
let value=1.5;
console.log(typeof(value))
Output: number
let bool=true;
console.log(typeof(bool))
Output: boolean

Parsing Datatypes

To convert integer type digits to string:


value.toString(); 
Or 
String(value);

To convert string type digits to integer:

parseInt(value);

To convert string type digits to float:

parseFloat(value);

To convert float type to integer:

Math.floor(value);
Math.ceil(value);
Math.round(value);
Math.trunc(value);

To fix the decimal value:

value.toFixed(number);

To precise the decimal value:

value.toPrecision(number);

String concat

“+” plus operator will be used to concat two or multiple variables.
Example:

let firstName="Shakib";
let lastName="Khan";
console.log(firstName+" "+lastName)

Or,
`${}` sign will be used to concat variables values in a string.
Example:

let firstName="Shakib";
let lastName="Khan";
console.log(`${firstName} ${lastName}`)

JS Operators

There are 6 types of operators

  1. Assignment operators
  2. Comparison operators
  3. Logical operators
  4. Type operators
  5. Bitwise operators
  6. Arithmetic operators

Assignment Operators

OperatorExample
=x = y
+=x += y
-=x -= y
*=x *= y
/=x /= y
%=x %= y
**=x **= y

Comparison Operators

OperatorDescription
==equal to
===equal value and equal type
!=not equal
!==not equal value or not equal type
>greater than
<less than
>=greater than or equal to
<=less than or equal to

Logical Operators

OperatorDescription
&&logical and
||logical or
!logical not

Type operators

OperatorDescription
typeOf()Returns the type of a variable
instanceOf()Returns true if an object is an instance of an object type

Bitwise Operators

OperatorDescriptionExampleSame asResultDecimal
&AND5 & 10101 & 00011 1
|OR5 | 10101 | 0001101 5
~NOT~ 5 ~01011010 10
^XOR5 ^ 10101 ^ 0001100 4
<<Zero fill left shift5 << 10101 << 11010 10
>>Signed right shift5 >> 10101 >> 110  2
>>>Zero fill right shift5 >>> 10101 >>> 110  2

Arithmetic Operators

OperatorDescriptionExampleSame asResultDecimal
&AND5 & 10101 & 00011 1
|OR5 | 10101 | 0001101 5
~NOT~ 5 ~01011010 10
^XOR5 ^ 10101 ^ 0001100 4
<<Zero fill left shift5 << 10101 << 11010 10
>>Signed right shift5 >> 10101 >> 110  2
>>>Zero fill right shift5 >>> 10101 >>> 110  2

Necessary Math Functions

Getting absolute value:
Math.abs(value);
Getting power of a number:
Math.pow(value,power);
Getting square root of a number:
Math.sqrt(value);
Getting random number:
Math.random()
Getting max/min from numbers:
Math.max(n1,n2,n3,,,,,,,,n);
Math.min(n1,n2,n3,,,,,,,,,n);

about author

admin

salmansrabon@gmail.com

If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

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