Setup Node.js and Npm 'Proxy' config if you are behind a proxy


Are you getting below error while using the npm?

"Error: connect ECONNREFUSED" and "If you are behind a proxy, please make sure that the
'proxy' config is set properly.  See: 'npm help config'".  As shown in below image


Node.js proxy Error

If you get above error, then you are proxy might not set properly for npm.

To Set the proxy for the Node.js, use below syntax
npm config set proxy "http://<PROXY_SERVER>:<PORT>"

We can also Pass username and password, If Proxy needs it like below
npm config set proxy http://username:password@<PROXY_SERVER>:<PORT>

In some cases, we might be using special characters for our passwords(like @ in password), in this case above syntax wont work. So its better to use double quotes (") for username:password as shown below.
npm config set proxy http://"username:password"@<PROXY_SERVER>:<PORT>

We can also use below to set https_proxy
npm config set https-proxy http://<PROXY_SERVER>:<PORT>
 

Arrays in JavaScript with Examples


Arrays

 An Array is an Object which stores multiple elements in it. it can be same type or different types.

Creating Arrays in JavaScript

    In JavaScript we can create array in 2 ways
    1. Using Array Literal Notation
    2. Using Array() Constructor

Creating Arrays using Array Literal Notation

    In this method of creating Array we simply use Square brackets and then put all the elements inside this using comma separated.
    Ex:-
  var arrLit1 = [];    //This is Empty Array
  var arrLit2 = [1,2,3];   //Array with numbers and length 3
  var arrLit3 = ["Foo","Bar"];//Array with Strings and length 2
 

Creating Arrays using Array() Constructor

    In this method We need to explicitly define the Array() with javascript's "new" Keyword
For the Array() Constructor, If we pass don't pass any arguments, then Array will be created with Zero length. If we pass one argument(Number) to the Array(), Array will be created with the passed argument as length. If we specify multiple arguments inside the constructor, then array will be created with those arguments and number of arguments wil be the lenght of the array.
   
    Ex:-
  var arr1 = new Array();   //This is Empty Array
  var arr2 = new Array(10);   //Array with length 10
  var arr3 = new Array("foo");  //Here Array with foo element created which is of length 1
  var arr4 = new Array(1,4,3); //Array with specified elements and length 3
 
       
We can create An array with mixed data types as shown in below example

 var arr3 = new Array("ss",1);
   

Array Length

    "length" is the property of Array. It will return the number of elements it currently holding or the length defined using the Array() constructor while creating the array.

    Ex:-
  var arr = new Array("foo","bar");
  console.log(arr.length) ; // It will print 2 in the console.
  
  var arr = new Array(10);
  console.log(arr.length) ; // It will print 10 in the console.
 

How to Access Contents of an array?

    To access the contents of an array in JavaScript we need to use the index.
    INDEX specifies the position of the element in the Array. The first element has an index of 0.
   
    Ex:-
  var arr = new Array("foo","bar");
  console.log(arr[0]); //prints "foo"
  arr[0] = "newVal";  //0 th index element(1st element) value replaced with "newVal"
 

JavaScript Timers - setTimeout() and setInterval() functions



The setTimeout() and setInterval() methods allows to schedule timer-based callbacks in JavaScript

setTimeout() 

Executes a callback function after a specified number of milliseconds.

Javascript setTimeout takes 2 parameters, one is Callback function and then the delay in Milliseconds.
The syntax is:
var timerId = setTimeout(callback, delay)
setTimeout() Example:
function foo() {
   alert('Hi')
}
setTimeout(foo, 1000);

We can also Cancel the execution of the setTimeout in JavaScript.
To Cancel the Execution we need to store the value returned by setTimeout and then call clearTimeout().

Example below for Cancelling the Clear timeout.
    function foo() { 
      alert('Hi');
    }
    var timeID = setTimeout(foo, 1000);
    clearTimeout(timeID);

setInterval() 

Executes a callback function at specified time intervals.
The syntax is:
    var timerId = setInterval(callback, interval);
setInterval() Example:
    function foo() { 
      alert('Hi');
    }
    setInterval(foo, 1000);

For setTimeout() we have cancel function clearTimeout(). for setInterval() also we have Cancel function available clearInterval().
Same like setTimeout(), to Cancel the Execution of setInterval(), we need to store the value returned by setInterval and then call setInterval().

Example below for Cancelling the Clear timeout.

    function foo() { 
      alert('Hi');
    }
    var timeID = setInterval(foo, 1000);
    clearInterval(timeID);