JAVASCRIPT interview questions part-I

1. What’s relationship between JavaScript and ECMAScript?
Ans.
ECMAScript is yet another name for JavaScript (other names include LiveScript). The current JavaScript that you see supported in browsers is ECMAScript revision 3.

2. Which types javascript supports?
Ans. Number, String, Boolean, Function, Object, Null, Undefined.

3. How do you convert numbers between different bases in JavaScript?
Ans. 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);

4. What does isNaN function do?
Ans. Return true if the argument is not a number.

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

6. What boolean operators does JavaScript support?
Ans. &&, || and !

7. What does “1″+2+4 evaluate to?
Ans. Since 1 is a string, everything is a string, so the result is 124.

8. How about 2+5+”8″?
Ans. Since 2 and 5 are integers, this is number arithmetic, since 8 is a string, it’s concatenation, so 78 is the result.

9. What looping structures are there in JavaScript?
Ans. for, while, do-while loops, but no foreach.

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

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

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

13. What is “this” keyword?
Ans. It refers to the current object.

14. What is difference between window.onload and onDocumentReady?
Ans. 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 isn’t 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.

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

16. What is the difference between undefined value and null value?
Ans. 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.

17. How do you change the style/class on any element?
Ans.
document.getElementById(“myText”).style.fontSize = “20?;
//-or-
document.getElementById(“myText”).className = “anyclass”;
18. What are Javascript closures?When would you use them?
Ans. Two one sentence summaries:
=> A closure is the local variables for a function – kept alive after the function has returned, or
=> A closure is a stack-frame which is not deallocated when the function returns.
A closure takes place when a function creates an environment that binds local variables to it in such a way that they are kept alive after the function has returned. A closure is a special kind of object that combines two things: a function, and any local variables that were in-scope at the time that the closure was created.
The following code returns a reference to a function:
function sayHello2(name) {
var text = ‘Hello ‘ + name; // local variable
var sayAlert = function() { alert(text); }
return sayAlert;
}
Closures reduce the need to pass state around the application. The inner function has access to the variables in the outer function so there is no need to store the information somewhere that the inner function can get it.
This is important when the inner function will be called after the outer function has exited. The most common example of this is when the inner function is being used to handle an event. In this case you get no control over the arguments that are passed to the function so using a closure to keep track of state can be very convenient.

19. What is unobtrusive javascript? How to add behavior to an element using javascript?
Ans. Unobtrusive Javascript refers to the argument that the purpose of markup is to describe a document’s structure, not its programmatic behavior and that combining the two negatively impacts a site’s maintainability. Inline event handlers are harder to use and maintain, when one needs to set several events on a single element or when one is using event delegation.
<input type="text" name="date" />
Say an input field with the name “date” had to be validated at runtime:
document.getElementsByName("date")[0].
addEventListener("change", validateDate, false);
function validateDate(){
// Do something when the content of the 'input' element with the name 'date' is changed.
}
Although there are some browser inconsistencies with the above code, so programmers usually go with a javascript library such as JQuery or YUI to attach behavior to an element like above.

1 comment: