Sunday, 13 January 2013

JavaScript Interview Questions

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.
That is not what we want. We just want to wait until the DOM is loaded and is able to be manipulated. onDocumentReady allows the programmer to do that.

What the difference is between == and ===?
The == checks for value equality, but === checks for both type and value.

What do “1″+2+4 evaluate to? What about 5 + 4 + “3″?
Since 1 is a string, everything is a string, so the result is 124.
In the second case, it wills 93.

What is the difference between undefined value and null value?
undefined means a variable has been declared but has not yet been assigned a value. On the other hand, null is an assignment value. It can be assigned to a variable as a representation of no value.
Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.
Unassigned variables are initialized by JavaScript with a default value of undefined. JavaScript never sets a value to null. That must be done programmatically.

How do you change the style/class on any element?
document.getElementById(“myText”).style.fontSize = “20″;
document.getElementById(“myText”).className = “anyclass”;

What data types are supported in JavaScript?
Number, String, Undefined, null, Boolean.

What is the difference between innerHTML and append() in JavaScript?
InnerHTML is not standard, and it’s a String. The DOM is not, and although innerHTML is faster and less verbose, its better to use the DOM methods like appendChild(), firstChild.nodeValue, etc to alter innerHTML content.

Differentiate between “var a=2” and “a =2”?
The major difference between the two is that one variable is local and the other is global. “var” basically defines the scope of the variable.
When we add var to a variable value assignment, JavaScript ensures that the variable is confined to whichever function it is assigned to and does not collide with the same name variable within another function.
When we don’t use var, then it is declared as a global function and chances of collision can happen. So it’s always advisable to use “var” before variable value assignment. If needed use an anonymous function for closure.

Can you explain what isNaN() function does?
isNaN() function will check an argument and return TRUE (1) if the argument does not seem to be a number.

Are JavaScript and jQuery different?
JQuery is a quick as well as concise JavaScript Library that simplifies HTML document traversing, animating, event handling, & Ajax interactions for the purpose of quick web development needs. So although they are not entirely different, both are not the same either!

Explain JavaScript closures:
A basic overview of JavaScript closures is that it is a stack-frame which is not de-allocated when the function returns.

How do you submit a form using JavaScript?
Use document.forms[0].submit();
(0 refers to the index of the form – if you have more than one form in a page, then the first one has the index 0, second has index 1 and so on).

How do you convert numbers between different bases in JavaScript?
Use the parseInt() function, that takes a string as the first parameter, and the base as a second parameter. So to convert hexadecimal 3F to decimal, use parseInt ("3F", 16);

What is negative infinity?
It’s a number in JavaScript, derived by dividing negative number by zero.

How do you create a new object in JavaScript?
var obj = new Object();
//-- or --
var obj = {};

How do you assign object properties?
obj["age"] = 17
//-- or --
obj.age = 17.

What’s a way to append a value to an array?
arr[arr.length] = value;

What is this keyword?
It refers to the current object.

How to read and write a file using JavaScript?
I/O operations like reading or writing a file is not possible with client-side JavaScript.

How to get the contents of an input box using JavaScript?
Use the "value" property.
var MyValue = window.document.getElementById("textboxID").value;

How to determine the state of a checkbox using JavaScript?
var CheckedP = window.document.getElementById("CheckBoxID").checked;

How to set the focus in an element using JavaScript? 
function SetFocus()
If (focusElement != null) {

What is the difference between an alert box and a confirmation box?
An alert box displays only one button which is the OK button whereas the Confirm box displays two buttons namely OK and cancel.

What is a prompt box?
A prompt box allows the user to enter input by providing a text box.

Can JavaScript code be broken in different lines?
Breaking is possible within a string statement by using a backslash \ at the end but not within any other javascript statement.
document.write("Hello \ world");
is possible but not document.write \
("hello world");

How to disable an HTML object?
document.getElementById("myObject").disabled = true;

How to get value from a textbox?

How to get value from dropdown (select) control?

No comments:

Post a Comment