Javascript Interview Questions and Answers - Freshers & Experienced

What are the different actions that are performed using JavaScript?

- JavaScript allows the computer to be more secure by putting the privacy policies and disable the unauthorized access to the files.
- It allows many actions to be performed like, setting the browser's preferences and receiving the request from the servers for the client computer.
- The settings that are used gets saved on the client side on the features like actions buttons, appearance and printing.
- It allows easy launching of an application on the client computer with interactive data to be communicated between the server and the client.
- It allows reading and writing of files or directories on the client or server side.
- It allows easy capturing of the data that is live streamed from the server to the client machine for retransmission.
- It also allows to safe and secure the application from the outside world.

How can JavaScript language be separated from objects?

JavaScript treats and creates the applications for the scripting to make the browser's compatible for use. The language is separated from the objects as it allows the syntax to change the environment. It is a language that keeps the page element in the HTML document. JavaScript allows the elements of the page to remain in sync with the document objects. The language is used to create objects that are connected to page elements and other elements in a language. The separation allows the concept of development and effort to be shared with each factor. The JavaScript language allows dynamic data to be presented using the weakly typed language. It also support any action to be taken to support user interface and graphics.

What is the use of DOM?

DOM is also known as Document Object Model which is used to develop a model with documents or web pages containing objects like elements, links, etc. These objects can be manipulated or certain actions like add, delete or change of an element can be performed using this document object model. Through this change in attributes can be done to get all the list of all the elements in the document. The DOM model responds to API calls that result in documented level of DOM recommendation. It is used to support additional behavior on the web page and use of API give an extensible advantage over other models existing. DOM codes are reused to meet the requirement of the real world and to make all the program interoperable.

What is the importance of <SCRIPT> tag?

- JavaScript is used inside <SCRIPT> tag in HTML document. The tags that are provided the necessary information like alert 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.
- The example is given as :
<SCRIPT> // Starting of the scripting language
//Your code here
</SCRIPT> --> // End of the scripting language
// your code here

Why JavaScript is called as Script for all browsers?

JavaScript is written after <SCRIPT> tag and it is surrounded in between the <!-- your code --> tags, this is also known as comment tag. JavaScript interpreter treats the tag such that it treats all the lines in the comments as script lines. The JavaScript comment starts with // inside the <SCRIPT> tag. The script is contained inside <HTML> tag that contains a comment tag. The browser that is non-compatible with JavaScripts ignore the lines and move on, but compatible browsers always treats it as a script and execute it. The browser treat the written lines between the comment tag as normal lines and just thinking of the comment ignores it. Some browsers just treat the <!-- comment--> as a comment only and ignores whatever is written inside it.

What are the requirements of Web application using JavaScript?

There are lots of application that require certain things when the user uses a JavaScript like:
Data entry validation : This tell that if the field of the form is filled out then during the processing of the server the client side can interact with it.
Serverless CGIs : This describes the processes that are not used with JavaScript but programmed as CGI on server, it gives low performace due to more interaction between the applicatioin and the user.
Dynamic HTML interactivity : It allows dynamic position of the data without using any other scripting language.
CGI prototyping : It allows more reduction of time to access the user interface before implementing the application of the CGI.

What are the different objects used in JavaScripts?

JavaScript uses a hierarchical structure that applies to the objects in a document. There are some objects that show the relationship of one object to another using the language.
Window object :
This is the topmost object in the hierarchy. It represent the content area of browser window that consists of HTML documents. Each frame is also a window that has some actions inside it.
Document object :
This object gets loaded in a window and consists of objects of different kind in the model. It consists of the content that will be written in the script.
Form object :
Form objects are used for more interaction with the users. It represents the form elements inside <FORM>...</FORM> tag.

Why is object naming important to use in JavaScript?

- 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. The example is :
<form name=”dataEntry” method=get>
<input type=”text” name=”entry”>
<frame src=”info.html” name=”main”>
- The names act as a nametags through which the elements can be easily identified and easily located by the browsers. The references made for each object includes the object hierarchy from the top down. References are used to include names of each object that are coming in the window object. The object naming conventions are easy way to loacte the objects and the linking between them can be done more comfortably.

What are the methods involved in JavaScript?

- 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. There is a method that is used by the JavaScript statement that includes a reference to an object this is given as :
- These are the functions which perform the dynamic interaction with the user. The first statement execute the element when pressed submit button to send a form to a server. These two statements are related to only the form. The scripts that are invoked will have the write of the document as well and will be written as :
document.write(“Give the version “ + navigator.appVersion)
document.write(“ of <B>” + navigator.appName + “</B>.”)

Explain with an example the use of event handlers in JavaScript.

The events in JavaScript are the actions in a document that result from user activity. The actions are like clicking on a button or typing a character in the form. JavaScript object in a document that receives events of different kinds. To handle the events that are taking place requires an even handler that can handle the execution of the events. Event acts like an added attribute that is entered in object’s HTML. The attribute is consisting of event name, sign like (=), instructions. The following code shows the event handler as :
<INPUT TYPE=”button” VALUE=”Hello” onClick=”window.alert (‘HELLO WORLD’)”>
1 2 3 4

JavaScript vs. Jscript
Both JavaScript and Jscript are almost similar. Java script was developed by Netscape...
Difference between Client side JavaScript and Server side JavaScript
difference between Client side JavaScript and Server side JavaScript - ...
JavaScript - Where are cookies actually stored on the hard disk?
Where are cookies actually stored on the hard disk? - ...
Post your comment
Discussion Board
Javascript interview questions and answers
Are there any predefined constant provided by the browser with the key code values that can be reused?
Use of global object, named as KeyEvent object, used in firefox browser, providing set of pre-defined constants that reflect the keys, which are used on keyboards. The constant’s name used is: DOM_VK_KEYNAME, with values representing the keydown/keyup key codes for the respective keys. For example, DOM_VK_SHIFT is 16, DOM_VK_ESCAPE is 27.

How can I set up my own JavaScript error handler?
To set up your own JavaScript error handler some optional parameters has to be known. These parameters are as follows:
- Textual description of error
- Address (URL) of page on which error occurred
- Number of line in which error occurred
If you want to invoke the default error handler of the browser, then your function should return (false) or vice versa. Example code:
function handlerFunction(description,page,line)
{ // put error-handling operators here
return true;}

How do I use JavaScript to password-protect my Web site?
There are several ways in which you can use JavaScript to password-protect your website. This can be done by setting the password by using the given name or pathname of HTML file on your site. Set the location to value entered in the field. Entry of wrong password will result in “invalid URL” error. If password protect pages requires more security then you can set temporary cookies on the page.

How can I prevent others from reading/stealing my scripts or images?
There are no assertive measures which can be taken to foolproof your scripts and images, but preventive measures can be taken like copyrighting your pages, putting watermark on your images and applying non-technological way to protect your images. Scripts are difficult to protect as they can be accessed through many applications and many programs by using the web browsers.
Rohit Sharma 12-11-2011
JavaScript interview questions and answers
How do I write script-generated content to another window?
You can use the methods winRef.document.writeln() or winRef.document.write() to write the script-generated content to another window. winRef stands for windows reference, it is being returned by method. Use of winRef.document.close() can be used if you want your script’s output to show up. Example code:
writeConsole('Hello world!');
function writeConsole(content) {'','myconsole', 'width=350,height=250'
top.consoleRef.document.writeln( '<html><head><title>Console</title></head>' +' top.consoleRef.document.close() }

How can I request data from the server without reloading the page in the browser?
JavaScript code which is being present and loaded in client browser, can request for data from the web server using XMLHttpRequest object. method is used to open the connection, not to send the request to web server. But, use of the function XMLHttpRequest.send() sends the request in real time. Example code is given below as:
var oRequest = new XMLHttpRequest();
var sURL = "http://"+ self.location.hostname + "/hello/requested_file.htm";"GET",sURL,false);
if (oRequest.status==200) alert(oRequest.responseText);
else alert("Error executing XMLHttpRequest call!");

How do I add a JavaScript event handler to an HTML page element?
You can use inline event handlers to add a JavaScript handler to an HTML page element. The disadvantage of this technique is that it allows you to have one handler per element. There are different browsers which allow you to have dynamic handler added to the HTML page element. Example of inline event handler is given below:
<a href="ineh.htm" onlick="alert('Hello!')">Good Morning!</a>
// event handlers added by assignment (usually right after the page loads), e.g.:

Rohit Sharma 12-11-2011
JavaScript interview questions and answers
How do I retrieve a cookie with a given name using a regular expression?
You can use readCookie() function to read the cookie. It provides sufficient arguments that one can find it, flexible to use. It takes cookieName as a parameter and other statements in the block. Below function uses the cookie function as well as regular expression to show the functionality:
function readCookie(cookieName)
{ var rx = new RegExp('[; ]'+cookieName+'=([^\\s;]*)');
var sMatch = (' '+document.cookie).match(rx);
if (cookieName && sMatch) return unescape(sMatch[1]);
return '';

What value does prompt() return if the user clicked the Cancel button?
Return value of prompt() function depends on browsers. Most of the browsers return the value as null and some return as empty string (“ “). IE is one of the browser which gives the error of empty string when clicked the cancel button by the user, otherwise all the recent browser return the value as null. The code to check this is as follows:
userInput = prompt('Prompt text','Suggested input');
if (userInput) {
// do something with the input

Why does the browser display the slow script warning?
When JavaScript on your browser is running but, it is not responding from a long time, then browser may display a warning message and give the user an option to terminate the script. Example, while loading a video application on Internet Explorer 8.0, it displays Yes/No dialog message like this:
Stop running this script?
A script on this page is causing Internet Explorer to run slowly.
If it continues to run, your computer might become unresponsive.
Rohit Sharma 12-11-2011