Wednesday 29 May 2013

Commonly asked JavaScript interview questions

1. What is the difference between == and === in javascript ?
The 3 equal signs mean "equality without type coercion". Using the triple equals, the values must be equal in type as well.
0==false   // true
0===false  // false, because they are of a different type
1=="1"     // true, auto type coercion
1==="1"    // false, because they are of a different type

Null and Undefined types are == (but not ===). [i.e. Null==Undefined (but not Null===Undefined)]

2. null vs undefined in Javascript ?
In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such as:

var TestVar;
alert(TestVar); //shows undefined
alert(typeof TestVar); //shows undefined
null is an assignment value. It can be assigned to a variable as a representation of no value:

var TestVar = null;
alert(TestVar); //shows null
alert(typeof TestVar); //shows object

From the preceding examples, it is clear that undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.

3. What are global variables and how are they declared? Also, explain problems using globals?
Global variables are available throughout your code: that is, the variables have no scope. Local variables scope, on the other hand, is restricted to where it is declared (ex-within a function). The var keyword is used to declare a local variable, while omitting the var keyword creates a global variable.

// Declare a local variable
var localVariable = "TechRepublic"
// Declare a global
globalVariable = "NewDelhi"

Most JavaScript developers avoid globals. One reason why is they’re averse to naming conflicts between local and globals, Also, code that depends on globals can be difficult to maintain and test.

4. How do you handle errors in JavaScript?
Exceptions that occur at runtime can be handled via try/catch/finally blocks. This can avoid unfriendly error messages. The finally block is optional and execution is transferred to the catch block of code when/if runtime errors occur. When the try/catch block is finally done, code execution transfers to the finally code block. This is the same way it works in other languages like C# and Java.

try {
// do something
} catch (e) {
// do something with the exception
} finally {
// This code block always executes whether there is an exception or not.
}

5. The onerror Event Handler
JavaScript 1.1 allowed a new event handler named "onerror". This Event Handler allows you to have something happen when the page throws an error.
The onerror event can only be defined inside your JavaScript code as a property of the window object:

<script type="text/javascript">
window.onerror=function(){
 //code to run when error has occured on page
}
</script>

6. Write a 1-line JavaScript code that concatenates all strings passed into a function?
The following function should help achieve the desired result

function concatenate() {
return String.prototype.concat.apply('', arguments);
}

7. What are windows object and navigator object in JavaScript?
The window object represents an open window in a browser.
Windows object is top level object in Java script and contains several other objects such as, document, history, location, name, menu bar etc., in itself. It is the global object for Java script that is written at client-side. For more details, click here.

The navigator object contains information about the browser.
navigator.appName - Gives the name of the browser
navigator.appVersion - Gives the browser version
navigator.appCodeName - Gives the browser codename
navigator.platform - Gives the platform on which the browser is running
For more details, click here.

8. Difference between window.onload and onDocumentReady?
The onload event does not fire until every last piece of the page is loaded, this includes css and images, which means there’s a huge delay before any code is executed.
If you just want to wait until the DOM is loaded and is able to be manipulated. onDocumentReady allows you to do that.


No comments:

Post a Comment