Javascript Interview Questions

JAVASCRIPT Interview Questions and Answers

1.How to detect the operating system on the client machine?

A.In order to detect the operating system on the client machine, the navigator.appVersion string (property) should be used.

2.What are JavaScript types?

A.Number, String, Boolean, Function, Object, Null, Undefined.

3.How do you convert numbers between different bases in JavaScript?

A.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.How do you target a specific frame from a hyperlink?

A.Include the name of the frame in the target attribute of the hyperlink. >My Page

5.What does isNaN function do?

A.Return true if the argument is not a number.

6.What is negative infinity?

A.It�s a number in JavaScript, derived by dividing negative number by zero.

7.In a pop-up browser window, how do you refer to the main browser window that opened it?

A.Use window.opener to refer to the main window from pop-ups.

8.What is the data type of variables of in JavaScript?

A.All variables are of object type in JavaScript.

9.How to reload the current page ?

A.window.location.reload(true);

10.How to create a confirmation box?

A.confirm("Do you really want to launch the missile?");

11.How to create an input box?

A.prompt("What is your temperature?");

12.How to disable an HTML object ?

A.document.getElementById("myObject").disabled = true;

13.What does the term sticky session mean in a web-farm scenario? Why would you use a sticky session? What is the potential disadvantage of using a sticky session?

A.Sticky session refers to the feature of many commercial load balancing solutions for web-farms to route the requests for a particular session to the same physical machine that serviced the first request for that session. This is mainly used to ensure that a in-proc session is not lost as a result of requests for a session being routed to different servers. Since requests for a user are always routed to the same machine that first served the request for that session, sticky sessions can cause uneven load distribution across servers.

14.You have an ASP.NET web application running on a web-farm that does not use sticky sessions - so the requests for a session are not guaranteed to be served the same machine. Occasionally, the users get error message Validation of viewstate MAC failed. What could be one reason that is causing this error?

A.The most common reason for this error is that the machinekey value in machine.config is different for each server. As a result, viewstate encoded by one machine cannot be decoded by another. To rectify this, edit the machine.config file on each server in the web-farm to have the same value for machinekey.

15.What is the difference between undefined value and null value?

A.(i)Undefined value cannot be explicitly stated that is there is no keyword called undefined whereas null value has keyword called null (ii)typeof undefined variable or property returns undefined whereas typeof null value returns object

16.Does javascript have the concept level scope?

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

17.What are undefined and undeclared variables?

A.Undeclared variables are those that are not declared in the program (do not exist at all),trying to read their values gives runtime error.But if undeclared variables are assigned then implicit declaration is done . Undefined variables are those that are not assigned any value but are declared in the program.Trying to read such variables gives special value called undefined value.

18.What is === operator ?

A.==== is strict equality operator ,it returns true only when the two operands are having the same value without any type conversion.

19.What looping structures are there in JavaScript?

A.for, while, do-while loops, but no foreach.

20.How to hide javascript code from old browsers that dont run it?

A.Use the below specified style of comments How to access an external javascript file that is stored externally and not embedded? where abc.js is the external javscript file to be accessed.

32.gfhgf

A.<html>

33.What is JavaScript?

A.JavaScript is a platform-independent,event-driven, interpreted client-side scripting language developed by Netscape Communications Corp. and Sun Microsystems.

34.What�s relationship between JavaScript and ECMAScript?

A.ECMAScript is yet another name for JavaScript (other names include LiveScript). The current JavaScript that you see supported in browsers is ECMAScript revision 3.

35.What is negative infinity?

A.It�s a number in JavaScript, derived by dividing negative number by zero.

36.Can you explain what isNaN function does?

A.isNaN function will check an argument and return TRUE (1) if the argument does not seem to be a number.

37.What is the difference between undefined value and null value?

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

38.What is Javascript namespacing? How and where is it used?

A.Using global variables in Javascript is evil and a bad practice. That being said, namespacing is used to bundle up all your functionality using a unique name. In JavaScript, a namespace is really just an object that you�ve attached all further methods, properties and objects. It promotes modularity and code reuse in the application.

39.What does "1"+2+4 evaluate to?

A.Since 1 is a string, everything is a string, so the result is 124.

40.How about 2+5+"8"?

A.Since 2 and 5 are integers, this is number arithmetic, since 8 is a string, its concatenation, so 78 is the result.

41.What is this keyword?

A.It refers to the current object.

42.How many looping structures can you find in javascript?

A.If you are a programmer, you know the use of loops. It is used to run a piece of code multiple times according to some particular condition. Javascript being a popular scripting language supports the following loops for while do-while loop

43.Are javascript and jQuery different?

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

44.Explain the strict mode in Javascript.

A.The strict mode ensures that if functions are not properly thought it, those are disabled. It also kept a check on potentially unsafe actions and throw errors when it happens.

45.Explain Javascript closures ?

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

46. What is event bubbling?

A.Event bubbling describes the behavior of events in child and parent nodes in the Document Object Model (DOM); that is, all child node events are automatically passed to its parent nodes. The benefit of this method is speed, because the code only needs to traverse the DOM tree once. This is useful when you want to place more than one event listener on a DOM element since you can put just one listener on all of the elements, thus code simplicity and reduction. One application of this is the creation of one event listener on a page�s body element to respond to any click event that occurs within the page�s body.

47.Difference between window.onload and onDocumentReady?

A.The onload event does not fire until every last piece of the page is loaded, this includes css and images, which means theres a huge delay before any code is executed.

48.How is form submission possible via javascript?

A.We can achieve the desired form submission by using the function document.forms[0].submit(). It must be noted that the 0 in the piece of code given above refers to the form index. Say we have multiple forms on a particular page. To make all the form procession unique, we give each form index numbers. The first form will have the index number as 0. The second form will have an incremented number, 1. The third will have 2 and so on.

49.How JavaScript timers work? What is a drawback of JavaScript timers?

A.Timers allow you to execute code at a set time or repeatedly using an interval. This is accomplished with the setTimeout, setInterval, and clearInterval functions. The setTimeout(function, delay) function initiates a timer that calls a specific function after the delay; it returns an id value that can be used to access it later. The setInterval(function, delay) function is similar to the setTimeout function except that it executes repeatedly on the delay and only stops when cancelled. The clearInterval(id) function is used to stop a timer. Timers can be tricky to use since they operate within a single thread, thus events queue up waiting to execute

50.How to detect the operating system on the client machine?

A.In order to detect the operating system on the client machine, the navigator.appVersion string (property) should be used.

51. What is the official JavaScript website?

A.This is a trick question used by interviewers to evaluate the candidate�s knowledge of JavaScript. Most people will simply say javascript.com is the official website. The truth is- there is no official website for Javascript you can refer to. It was developed by Brendan Eich for Netscape. It was based on the ECMAScript language standard; ECMA-262 being the official JavaScript standard.

52.What is the difference between == and === ?

A.The == checks for value equality, but === checks for both type and value.

53.What is the difference between innerHTML and append() in JavaScript?

A.InnerHTML is not standard, and its 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.

54.Can you write a function that takes an object and appends it to the DOM, making it so that events are buffered until the next tick? Explain why this is useful?

A.This last part only applies in browser-side settings where it can dramatically increase performance.

55.How to read and write a file using javascript?

A.I/O operations like reading or writing a file is not possible with client-side javascript. However , this can be done by coding a Java applet that reads files for the script.

56.How do you convert numbers between different bases in JavaScript?

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

57.How to create arrays in JavaScript?

A.We can declare an array like this : var scripts = new Array(); . We can add elements to this array like this

58.What is a fixed-width table and its advantages?

A.Fixed width tables are rendered by the browser based on the widths of the columns in the first row, resulting in a faster display in case of large tables. Use the CSS style table-layout:fixed to specify a fixed width table. If the table is not specified to be of fixed width, the browser has to wait till all data is downloaded and then infer the best width for each of the columns. This process can be very slow for large tables.

59.What can javascript programs do?

A.Generation of HTML pages on-the-fly without accessing the Web server. The user can be given control over the browser like User input validation Simple computations can be performed on the clients machine The users browser, OS, screen size, etc. can be detected Date and Time Handling

60.How to set a HTML documents background color?

A.document.bgcolor property can be set to any appropriate color.

61.In a pop-up browser window, how do you refer to the main browser window that opened it?

A.Use window.opener to refer to the main window from pop-ups.

62.What is the data type of variables of in JavaScript?

A.All variables are of object type in JavaScript.

63.Methods GET and POST in HTML forms - what is the difference?

A.GET: Parameters are passed in the querystring. 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

64.Is a javascript script faster than an ASP script?

A.Yes.Since javascript is a client-side script it does require the web servers help for its computation,so it is always faster than any server-side script like ASP,PHP,etc.

65.What is the difference between a web-garden and a web-farm?

A.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).

66.What is the difference between an alert box and a confirmation box?

A.An alert box displays only one button which is the OK button whereas the Confirm box displays two buttons namely OK and cancel.

67.What is a prompt box?

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

68.What is the difference between SessionState and ViewState?

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

69.Name the numeric constants representing max,min values?

A.Number.MAX_VALUE and Number.MIN_VALUE

70.What does javascript null mean?

A.The null value is a unique value representing no value or no object. It implies no object,or null string,no valid boolean value,no number and no array object.

71.What is tha way to append a value to an array?

A.arr[arr.length] = value;

72.Does javascript have the concept level scope?

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

73.What does the delete operator do?

A.The delete operator is used to delete all the variables and objects used in the program ,but it does not delete variables declared with var keyword.

74.What is the difference between JavaScript and Jscript?

A.Both JavaScript and Jscript are almost similar. JavaScript was developed by Netscape. Microsoft reverse engineered Javascript and called it JScript.

75.Is JavaScript case sensitive?

A.Yes! A function getElementById is not the same as getElementbyID.

76.If an array with name as "names" contain three elements, then how will you print the third element of this array?

A. Print third array element document.write(names[2]);

77.How do you submit a form using JavaScript?

A.Use : document.forms[0].submit();

78. What is the use of Math Object in JavaScript?

A.The math object provides you properties and methods for mathematical constants and functions.

79.Does JavaScript support foreach loop?

A.No.

80.What is the difference between Client side JavaScript and Server side JavaScript.

A.Client side java script comprises the basic language and predefined objects which are relevant to running java script in a browser. The client side java script is embedded directly by in the HTML pages.

81.What is the Dictionary class?

A.The Dictionary class is an abstract class. The class maps keys to values. The classes such as HashTable are the sub classes of the abstract class Dictionary. The key and values are objects. The key and value are non-null objects

82.What is decodeURI(), encodeURI() in JavaScript?

A.To send the characters that can not be specified in a URL should be converted into their equivalent hex encoding. To perform this task the methods encodeURI() and decodeURI() are used.

83.What is eval() in Javascript?

A.The eval() method is incredibly powerful allowing us to execute snippets of code during execution in javascript.

84.Is it possible to stop the clientside validation of an entire page?

A.Yes, : set Page.Validate equal to false

85.What can a JavaScript Do?

A.JavaScript gives HTML designers a programming tool - HTML authors are normally not programmers, but JavaScript is a scripting language with a very simple syntax! Almost anyone can put small "snippets" of code into their HTML pages JavaScript can put dynamic text into an HTML page.

86.What is the syntax of capture events method for document object?

A.captureEvents(eventType)

87.What is the JavaScript exception available to the Java code as an instance of?

A.netscape.javascript.JSException

88. When a JavaScript object is sent to Java, the runtime engine creates a Java wrapper of type?

A.JSObject

89.To set up the window to capture all Click events, we use which statement?

A.window.captureEvents(Event.CLICK);

90.Which method of an Array object adds and/or removes elements from an array?

A.Splice

91.What is the server-side JavaScript object?

A.File

92.What is the client-side JavaScript object?

A.FileUpLoad

93. Which types of image maps can be used with JavaScript?

A.Client-side image maps

94.What is JSON in Javascript?

A.JSON is a JavaScript Object Notation. It is language independent and derived from Java Script. This is a LightWeight scripting language designed for data interchange over a network. Basically this is used for data serialization and data transmission which helps to transfer data between the server and the web application.

95.How are errors gracefully handled in JavaScript?

A.Exceptions that occur at runtime can be handled via try/catch/finally blocks; this allows you to avoid those unfriendly error messages. The finally block is optional, as the bare minimum to use is try/catch. Basically, you try to run code (in the try block between the braces), 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.

96.What is the importance of <SCRIPT> tag?

A.JavaScript is used inside <SCRIPT> tag in HTML document. The tags that are provided provides the necessary information like alret to the browser for the program to begin interpreting all the text between the tags. The <script> tag uses JavaScript interpreter to handle the libraries that are written or the code of the program. JavaScript is a case sensitive language and the tags are used to tell the browser that if it is JavaScript enabled to use the text written in between the <Script> and </Script> tags.

97.What are the methods involved in JavaScript?

A.Method is an informative that gets performed over an action that is related to the object. Method either performs on some object or affect any part of the the script or a document. Object can have as many number of methods that have associations with other objects.

98.Why is object naming important to use in JavaScript?

A.Object naming is used to create script references to objects while assigning names to every scriptable object in the HTML code. The browsers that are script compatible looks for the optional tags and attributes that enables the assigning of a unique name to each object.

99.What does window.moveTo() method?

A.Move the current window.

100.What does window history back?

A.The history.back() method loads the previous URL in the history list.

101.What are javascript frameworks/libraries?

A.The most popular JavaScript frameworks: jQuery, Prototype and MooTools. All of these frameworks have functions for common JavaScript tasks like animations, DOM manipulation, and Ajax handling.

102.Describe YUI framework?

A.The Yahoo! User Interface Framework is a large library that covers a lot of functions, from simple JavaScript utilities to complete internet widgets.

103.Describe Ext JS framework?

A.Customizable widgets for building rich Internet applications.

104.Describe Dojo framework?

A.A toolkit designed around packages for DOM manipulation, events, widgets, and more.

105.Describe script.aculo.us framework?

A.Open-source JavaScript framework for visual effects and interface behaviors.

106.What is CDN?

A.CDN : Content Delivery Networks. You always want your web pages to be as fast as possible. You want to keep the size of your pages as small as possible, and you want the browser to cache as much as possible. If many different web sites use the same JavaScript framework, it makes sense to host the framework library in a common location for every web page to share.A CDN (Content Delivery Network) solves this. A CDN is a network of servers containing shared code libraries.

107.What try,catch and throw statements does in javascript?

A.The try statement lets you to test a block of code for errors. The catch statement lets you handle the error. The throw statement lets you create custom errors.

108.What try,catch and throw statements does in javascript?

A.The try statement lets you to test a block of code for errors. The catch statement lets you handle the error. The throw statement lets you create custom errors.

109.What try,catch and throw statements does in javascript?

A.The try statement lets you to test a block of code for errors. The catch statement lets you handle the error. The throw statement lets you create custom errors.

Comments