Javascript Interview Questions

Here are some of the javascript questions along with answers that you can refer to before appearing for the interview.

Also, if you are looking for jQuery then please read this.

jQuery Interview Questions

1. What’s relationship between JavaScript and ECMAScript? -
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. What are JavaScript types? -
Number, String, Boolean, Function, Object, Null, Undefined.

3. 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);

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

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

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

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

8. How about 2+5+"8"? -
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? -
for, while, do-while loops, but no foreach.

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

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

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

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

14. What is the difference between this() and super()?
this() can be used to invoke a constructor of the same class whereas super() can be used to invoke a super class constructor.

15. Where are cookies actually stored on the hard disk?
This depends on the user’s browser and OS.In the case of Netscape with Windows OS,all the cookies are stored in a single file called cookies.txt
c:\Program Files\Netscape\Users\username\cookies.txt
In the case of IE,each cookie is stored in a separate file namely username@website.txt.
c:\Windows\Cookies\username@Website.txt

16. How to set a HTML document's background color?
document.bgcolor property can be set to any appropriate color.

17. 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);

18. How to create arrays in JavaScript?
We can declare an array like this
var scripts = new Array();
We can add elements to this array like this
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
Now our array scrips has 4 elements inside it and we can print or access them by using their index number. Note that index number starts from 0. To get the third element of the array we have to use the index number 2 . Here is the way to get the third element of an array.
document.write(scripts[2]);
We also can create an array like this
var no_array = new Array(21, 22, 23, 24, 25);

19. How do you target a specific frame from a hyperlink?
Include the name of the frame in the target attribute of the hyperlink. >My Page

20. In a pop-up browser window, how do you refer to the main browser window that opened it?
Use window.opener to refer to the main window from pop-ups.

21. What is the data type of variables of in JavaScript?
All variables are of object type in JavaScript.

22. Methods GET and POST in HTML forms - what's the difference?
GET: Parameters are passed in the query string. Maximum amount of data that can be sent via the GET method is limited to about 2kb.
POST: Parameters are passed in the request body. There is no limit to the amount of data that can be transferred using POST. However, there are limits on the maximum amount of data that can be transferred in one name/value pair.

23. Is a JavaScript script faster than an ASP script?
Yes. Since JavaScript is a client-side script it does require the web server's help for its computation, so it is always faster than any server-side script like ASP, PHP, etc..

24. What is the difference between RegisterClientScriptBlock and RegisterStartupScript?
RegisterClientScriptBlock emits the JavaScript just after the opening tag. RegisterStartupScript emits the JavaScript at the bottom of the ASP. NET page just before the closing tag.

25. What is the difference between a web-garden and a web-farm?
Web-garden - An IIS6.0 feature where you can configure an application pool as a web-garden and also specify the number of worker processes for that pool. It can help improve performance in some cases.
Web-farm - a general term referring to a cluster of physically separate machines, each running a web-server for scalability and performance (contrast this with web-garden which refers to multiple processes on one single physical machine).

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

27. How to set the focus in an element using Javascript?


28. 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.

29. What would the output of 2+5+"8"?
Since 2 and 5 are integers, this is number arithmetic, since 8 is a string, it’s concatenation, so 78 is the result.

30. What is the difference between SessionState and ViewState?
ViewState is specific to a page in a session. Session state refers to user specific data that can be accessed across all pages in the web application.

31. Does JavaScript have the concept level scope?
No. Javascript does not have block level scope, all the variables declared inside a function possess the same level of scope unlike c, c++, java.

No comments: