Tuesday, November 10, 2015

JavaScript Interview Questions

When should you use var keyword in declaration and when should you not?
As a good practice, one should always use the var keyword. Not using var, creates a global variable. So, we should avoid avoiding var.
(function() {
   num1 = 1;
   var num2 = 2;
})();
console.log(num1); // outputs 1
console.log(num2); // error: num2 is not defined

What is prototypal inheritance in JavaScript?
JavaScript (ECMA5) does not have classes and does not support classical inheritance. Instead, each object can be a prototype of another object. Then, the object inherits the properties of its prototype. In below example, properties of animal are inherited by cow and the properties of cow are inherited by calf. So, calf has four legs same as cow and animal.
var animal = {legs:4};
var cow = Object.create(animal);
var calf = Object.create(cow);

Why would you include 'use strict' at the beginning of a JavaScript source file?
Using strict mode enforces stricter error checking at run time. It raises errors that would have otherwise failed silently. It helps to avoid simple mistakes like creating accidental globals, undefined values, or duplicate property names.

What datatypes are supported in JavaScript?
Number, String, Boolean, Object, Function, Null, Undefined

What is the difference between == and === ?
‘==’ evaluates equality of the value, while ‘===’ evaluates  equality of both type and value.

What is the  difference between window.onload() and the jQuery $(document).ready() method?
The window.onload method runs after all the assets (HTML, CSS, images) have loaded. The $(document).ready() runs as soon as the Document Object Model (DOM) is loaded.

How do you check if a variable is an object?
null is an object but null object is 'falsy'. So this works -
if(foo && typeof foo === "object") {
console.log('foo is object and is not null');
}

What are closures in JavaScript and how are they used?
A closure is an expression that can have free variables together with an environment that binds those variables.

What is the difference between Function as an expression and Function as a statement?
Function as an expression. Cannot be used before it is created. var add = function do(){};
Function as a statement. function do(){}; Can be called before it is created because it is pushed to top of scope.

Does JS need polymorphism?
JS parameters is English word. Arguments is a keyword. When parameters are passed to a function. JS passes one more parameter to the function called arguments. If you do console.log(arguments.length), it gives number of all the arguments passed. arguments is an array LIKE object. To fix max number of parameters inside function check arguments.length <= 1. arguments.push() does not work as arguments is not an array.

If you pass less number of parameters, JS does not complain.

If function does not have return statement, it returns undefined.

What are the different ways to create an object?
You can create and object in three possible ways -
- Object as a literal: var x = {a:'3', b:'4'};
- Object using a constructor
- Using Object.create

Whatever way and as soon as you create a function, always its prototype object gets created. In JS, an object creates another object. So, when you create a function, constructor in JS is not a concept, its just a function.

Object as a literal:
No matter how you create object with properties.JavaScript properties of an object are acually stored as an object. It works as an array of objects. To see the object run below code.
var x = {a:'3', b:'4'};
console.log(Object.getOwnPropertyDescriptor(x, 'a'));

//Make a property readonly
var x = {a:'3', b:'4'};
Object.defineProperty(x,'a', {writable:false});
console.log(Object.getOwnPropertyDescriptor(x, 'a'));

//To generate error if someone tries to overwrite this, write 'use strict'
'use strict'
var x = {a:'3', b:'4'};
Object.defineProperty(x,'a', {writable:false});
console.log(Object.getOwnPropertyDescriptor(x, 'a'));

//Make a json object
var x = {a:'3', b:'4'};
console.log(JSON.stringify(x));

//Make one property enumerable false. It does not show up in console. The property will be available by dot operator x.a but it cannot be enumerated in for loop. Using for loop it will not be available.
var x = {a:'3', b:'4'};
Object.defineProperty(x,'a', {enumerable:false});
console.log(JSON.stringify(x));
 
//Object using a constructor
function foo(who){
this.me = who;
}
var a1 = new foo("james");
var a2 = new foo("bond");
console.log(a1.me);
console.log(a2.me);
 //a1.[[Prototype]] gives foo.prototype.
 //As [[Prototype]] is hidden, Mozilla introduced __proto__
 //So, function has prototype, object has __proto__
 //In IE, __proto__ does not work so use, a1.contructor.prototype
 
 

No comments: