Friday, 25 December 2020

Node.js

 Node.js Basic

What is Node.js

  1. NodeJS is an open source, cross-platform, run-time environment and library for running      JavaScript applications outside the web browser. 
  2. It is used to create networking and server-side applications.
  3. It is primarily used in non-blocking, event-driven servers.
why use Node.js
Any programming language will give some reasons to choose them. Node.js is a programming technology which is able to run JavaScript code outside the web browser. 

  There are some advantages of using Node.js:
  • it makes possible to code in JavaScript for both client and server side.
  • It can speed up the development of real-time applications.
  • It increases the efficiency of development process by decreasing the gap between front-end and back-end developers.  

NPM

NPM(Node Package Manager) is the default package manager for Node.js. NPM allows programmers to install, update, and use open source packages.
NPM provides two major functionalities:
  1. NPM is an online repository for publishing open-source Node.js projects.
  2. NPM is a command line utility to install Node.js, version management, dependency management of Node.js packages.
NPM install package using simple syntax as follows:
    
                 npm install<package-name>

package.json file

package.json file contains all npm package files usually in the project root. It holds various metadata relevant to the project such as a project description, license information, version of the project in the particular distribution etc. and gives information to npm that allows it to identify the project as well as handle the project's dependencies.

Complete package.json of underscore as shown below:




Semantic Versioning

In every software development, semantic versioning provides the standard version as a number. Generally versioning have three digits like A.B.C .
  • A is the major version
  • B is the minor version
  • C is the patch version
When you make a new software, you can give version using following rules:
  1. When you make incompatible API changes, major version is updated.
  2. When you add functionality on previous released software, minor version is updated.
  3. When you make bug fixing, patch version is updated.

Package-lock.json file

It is generated automatically when npm modifies either the node_modules tree, or package.json.
It is solely used to lock dependency to specific version number.
It provides single representation of a dependency tree which makes easier for developer.
It optimizes the NPM installs by skipping processing of previously installed packages.

Sunday, 6 December 2020

 Variable Declaration and Initialization are separate

Declaration and initialization are separate terms. Declaration means to declare a variable with var keyword which assigns the memory allocation to store the variable, and initialization means assign the initial value to the variable before using it.

 simple example of variable declaration is:


 In the above example, we declare the variable 'y' but had not assigned any values to it, as a result it gives output as:



After assigning some value as in the example:




The output of the above example is:






Reference Error and Undefined

Reference error and undefined are two different things. Reference error arises when we try to use the variable without declaring it.

Below example shows the reference error:



        Error encountered as:




Undefined arises when we declare a variable but had not assigned any value. Following example clear the undefined concept:


 
the output of above example is:




Data Types in JavaScript

Data Types basically specifies what kinds of data are stored by a variable in the program. JavaScript provides different data types.
  • Object: represent complex data.
  • Null: represent unknown value.
  • Array: represent similar value.
  • String: represent string values.
  • Number: represent integer or floating value.
  • Boolean: represent true or false.
  • Undefined: represent undefined value.
In JavaScript, we do not need to explicitly define data types. They are automatically assigned to the variable as per the value they posses during run time.

more clear concept about data types through following example:




  





JavaScript is a Prototypical Language

JS is not a class based language but a prototype based language. A prototyped based language means any object can be assigned as the prototyped for another object. It uses classless inheritance programming style in creating duplicate object.

Let's understand more from basic example:



In the above example, there is a function Student which has two properties as firstName and lastName. we add another property faculty through prototype. Here we create two objects student1 and student2 which inherit the properties of Student.


       Output:

       

Creating Objects in JavaScript

There are two ways to creating the objects in JavaScript.
  1. By object literals
  2. By using object constructor   
The constant values that are assigned to the variable are called JavaScript literals.

In object literals we create and define object value separated by comma inside of curly braces. object literal can created by using following syntax:
  
    object={property1:value1,property2:value2,.....,propertyN:valueN}

A constructor is a method that instantiate an object.

In object constructor, we create a constructor function with properties. Each properties value need to assigned in the current object by this keyword. Object constructor can be created by using given syntax:
 
    function function_name(property1,property2,....propertyN)  {
     this.property1=property1;
     this.property2=property2;
      this.propertyN=propertyN;
  }
var object=new function_name(value1,value2,....valueN);

More clearly understand creation of object by following example:



Output becomes:



Friday, 4 December 2020

      JavaScript Introduction

    1. JavaScript is a loosely-typed client side scripting language that executes in the user's web browser.
    2. It is a lightweight, interpreted programming language with object-oriented capabilities.
    3. Designed for creating network-centric applications.

      JavaScript syntax

    1. JavaScript code can be written inside <script> tag.
    2. External JavaScript file (.js) can be referenced using <script src="/PathToScriptFile.js"></script> where src attribute is used to specify the full path of .js file.
    3. JavaScript ignores spaces, tabs, and newlines that appear in JavaScript programs.
    4. We can place the <script> tag anywhere within the html web page, but it is normally recommended that it should be kept within the <head> tag and end the body tag.


 JavaScript is Case Sensitive

JavaScript is a case sensitive language. This means that the language keywords, variables,  function names, and any others identifiers must always be typed with a consistent capitalization of letters.

 For example: online, Online, OnLine, and ONLINE are four distinct variable names.

    




JavaScript is a Dynamic Language

JavaScript is dynamically typed, that means types of variables are generally not known at compile time. 



 JavaScript Datatypes are Inferred

Type inference means you don't have to worry about declaring types everywhere.it automatically choose preferred datatype according to what is being assigned to variable.


In the above example, the 'x' variable contains string value as well as number value and displays both values one after another without any error.

Var for Declaration 

The var keyword is used to create new variables in the current scope. var respects the scope ,without var variables are global.



In above, variable 'x' is not recognized outside the scope because 'x' is local variable. so, it encountered the error as below:


If we had not defined 'x' variable using var, then 'x'  would become the global variable which could be accessed from everywhere.


JavaScript Hoisting

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.

for example: