How to Install Latest Nodejs & Npm on Ubuntu 16.10

Step 1: Add NodeJs PPA

First you need to node.js ppa in our system provide by nodejs official website. We also need to install python-software-properties package if not installed already.

$ sudo apt-get install python-software-properties
$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
Step 2: Install Node.js and NPM

After adding required PPA file, lets install Nodejs package. NPM will also be installed with node.js. This command will also install many other dependent packages on your system.

$ sudo apt-get install nodejs

Javascript Questions/Answers

Explain event delegation?

JavaScript event delegation is a simple technique by which you add a single event handler to a parent element in order to avoid having to add event handlers to multiple child elements.

More info: http://www.sitepoint.com/javascript-event-delegation-is-easier-than-you-think/

Explain how this works in JavaScript?

Within JavaScript your code will always have some form on context. The way context works is through the this variable. The this variable will always refer to the object that the code is currently inside of. Remember that global objects are actually properties of the window object. This means that even in a global context, the this variable will still refer to an object (BOM Browser Object Model || window object).

More info: https://justin.harmonize.fm/development/2009/09/26/an-introduction-to-javascripts-this.html

How do you go about testing your JavaScript?

I prefer QUnit because it’s developed by the team of jQuery.

More info: http://net.tutsplus.com/tutorials/javascript-ajax/how-to-test-your-javascript-code-with-qunit/
http://www.ibm.com/developerworks/web/library/wa-tools/

AMD vs. CommonJS?

CommonJS is a project to define a common API and ecosystem for JavaScript. One part of CommonJS is the Module specification. Node.js and RingoJS are server-side JavaScript runtimes, and yes, both of them implement modules based on the CommonJS Module spec.

AMD (Asynchronous Module Definition) is another specification for modules. RequireJS is probably the most popular implementation of AMD. One major difference from CommonJS is that AMD specifies that modules are loaded asynchronously – that means that modules are only loaded as they are needed, as opposed to loading all modules up front.

AMD is generally more used in client-side (in-browser) JavaScript development due to this, and CommonJS Modules are generally used server-side. However, you can use either module spec in either environment – for example, RequireJS offers directions for running in Node.js and browserify is a CommonJS Module implementation that can run in the browser

What is hash table?

It is also known as hash map is a data structure used to implement an associative array.It is a structure that can map keys to values.

How it works?

A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found.

See the below diagram it clearly explains

tiD5Z

 

Advantages:

In a well-dimensioned hash table, the average cost for each lookup is independent of the number of elements stored in the table.

Many hash table designs also allow arbitrary insertions and deletions of key-value pairs.

In many situations, hash tables turn out to be more efficient than search trees or any other table lookup structure.

Disadvantages:

The hash tables are not effective when the number of entries is very small. (However, in some cases the high cost of computing the hash function can be mitigated by saving the hash value together with the key.)

Uses:

They are widely used in many kinds of computer software, particularly for associative arrays, database indexing, caches and sets.

Explain why the following doesn’t work as an IIFE: function foo(){ }(); ?
What needs to be changed to properly make it an IIFE?

The Immediately-Invoked Function Expression (IIFE) has it’s syntax to work like: (function(){})(); so to make this function work it should be (foo(){})()

 

What’s the difference between a variable that is: null, undefined or undeclared?
How would you go about checking for any of these states?

Undeclared variables are those that are not declared in the program (do not exist at all),trying to read their values gives runtime error.But if undeclared variables are assigned then implicit declaration is done .
Undefined variables are those that are not assigned any value but are declared in the program.Trying to read such variables gives special value called undefined value.

while null is value of nothing that means the variable declared and have a null value.

To check we can use typeof operator like that:

if (typeof(a) == "undefined") {
  // this will be executed
  alert("a is undeclared or undefined");
}
else {
  alert("a is declared and defined");
}

 

What’s a typical use case for anonymous functions?

we can use them as callback functions as you can pass it as a parameter to an other function.

What’s the difference between host objects and native objects?

Both terms are defined in the ECMAScript specfication:

native object
object in an ECMAScript implementation whose semantics are fully defined by this specification rather than by the host environment.

NOTE Standard native objects are defined in this specification. Some native objects are built-in; others may be constructed during the course of execution of an ECMAScript program.

host object
object supplied by the host environment to complete the execution environment of ECMAScript.

NOTE Any object that is not native is a host object.

A few examples:

Native objects: Object (constructor), Date, Math, parseInt, eval, string methods like indexOf and replace, array methods, …

Host objects (assuming browser environment): window, document, location, history, XMLHttpRequest, setTimeout, getElementsByTagName, querySelectorAll, …

What’s the difference between .call and .apply?

The main difference is that apply lets you invoke the function with arguments as an array; call requires the parameters be listed explicitly.

Pseudo syntax:

theFunction.apply(valueForThis, arrayOfArgs)

theFunction.call(valueForThis, arg1, arg2, …)

 

Explain Function.prototype.bind?

Creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

var x = 9; 
var module = {
  x: 81,
  getX: function() { return this.x; }
};

module.getX(); // 81

var getX = module.getX;
getX(); // 9, because in this case, "this" refers to the global object

// create a new function with 'this' bound to module
var boundGetX = getX.bind(module);
boundGetX(); // 81

 

Difference between document load event and document ready event?

The ready event occurs after the HTML document has been loaded, while the onload event occurs later, when all content (e.g. images) also has been loaded.

The onload event is a standard event in the DOM, while the ready event is specific to jQuery. The purpose of the ready event is that it should occur as early as possible after the document has loaded, so that code that adds funcionality to the elements in the page doesn’t have to wait for all content to load.

 

What is the arity of a function?

the number of formal parameters defined for a function

A function can determine its own arity (length) like this:

// For IE, and ES5 strict mode (named function)
function foo(x, y, z) {
    return foo.length; // Will return 3
}

// Otherwise
function bar(x, y) {
    return arguments.callee.length; // Will return 2
}

 

What’s the difference between an “attribute” and a “property”?

Attributes carry additional information about an HTML element and come in name=”value” pairs

Property is a representation of an attribute in the HTML DOM tree. So the attribute in the example above would have a property named className with a value of my-class

So Attributes are in your HTML text document/file, whereas properties are in HTML DOM tree