Sunday, March 15, 2015

Difference between == and === in JavaScript

== converts the operands to the same type before checking for quality.
== checks only for same value.

Example


=== does not converts the operands to the same type before checking for quality.
=== checks for same value + same type. 

Example


Link of Examples
== Comparison Operator
=== Comparison Operator

Sunday, December 21, 2014

Prototypal Inheritance in JavaScript

Prototypal Inheritance in JavaScript

Link 1 - Prototypal Inheritance

Closure in JavaScript

Closure is when a function remembers its lexical scope even when the function is executed outside its lexical scope.
We can make private variable with closure.

For Example

Link 1 - Closure
Link 2 - Closure
Link 3 - Closure
Link 4 - Closure

Hoisting in JavaScript

Hoisting in JavaScript

Moving the declaration of variable and function at the top is called Hoisting.
First function declaration is hoisted and then variable declaration is hoisted.

Example 1

In the above program JavaScript compiler moves declaration of variable at the top.

Example 2

In the above program JavaScript compiler moves declaration of variable and function at the top.

Saturday, December 20, 2014

Function Declaration and Function Expression in JavaScript

Function Declaration

function keyword is the first word in statement then it is a function declaration.
Just as variable declaration must start with "var", function declaration must begin with "function".


Function Expression

function keyword is not the first word in statement then it is a function expression.
function expression can be Anonymous function expression or named function expression



Link 1 - Function Declaration
Link 2 - Anonymous Function Expression
Link 3 - Named Function Expression
 

New Keyword in JavaScript

The new keyword create an instance of a user-defined object.
The new keyword create an instance of a built-in object types that has a constructor function.

Below program create an instance of a user-defined object.

Below program create an instance of a built-in object types that has a constructor function.

New operator takes a function constructor as a argument and create a new object which is extend from the prototype property of the function constructor and then execute the function constructor with the content of the newly created object and then return the newly created object.


this value with new keyword

When a function is used as a constructor (with the new keyword), its "this" is bound to new object being constructed.


Link 1 - new keyword
Link 2 - new keyword
Link 3 - new keyword
Link 4 - new keyword

Friday, December 19, 2014

bind() method in JavaScript

bind() method - Hard binding

bind() method creates a new function that will have "this" set to the parameter passed to bind.
bind() method is used when we want "this" value to be predictable

In the above program displayName() method always has obj1 as "this" value.


Link 1 - Hard binding
Link 2 - Hard binding
Link 3 - Hard binding
Link 4 - Hard binding