Do you know that? Think of the user and NOT yourself while designing. next

See right interview answers on 30 common job interview questions


Collapse | Expand

1. 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. that is ,

document.write("Hello \ world");

is possible but not document.write \ ("hello world");

2. How do we get JavaScript onto a web page?

You can use several different methods of placing javascript in you pages. You can directly add a script element inside the body of page.

1. For example, to add the "last updated line" to your pages, In your page text, add the following:

<p>blah, blah, blah, blah, blah.</p>

<script type="text/javascript" >

<!-- Hiding from old browsers document.write("Last Updated:" + document.lastModified);

document.close(); // -->

</script> <p>yada, yada, yada.</p>

(Note: the first comment, "<--" hides the content of the script from browsers that don't understand javascript. The "// -->" finishes the comment. The "//" tells javascript that this is a comment so javascript doesn't try to interpret the "-->". If your audience has much older browsers, you should put this comments inside your javascript. If most of your audience has newer browsers, the comments can be omitted. For brevity, in most examples here the comments are not shown. ) The above code will look like this on Javascript enabled browsers, 

2. Javascript can be placed inside the <head> element Functions and global variables typically reside inside the <head> element. <head> <title>Default Test Page</title> <script language="JavaScript" type="text/javascript"> var myVar = ""; function timer(){setTimeout('restart()',10);} document.onload=timer(); </script> </head> Javascript can be referenced from a separate file Javascript may also a placed in a separate file on the server and referenced from an HTML page. (Don't use the shorthand ending "<script ... />). These are typically placed in the <head> element. <script type="text/javascript" SRC="myStuff.js"></script>

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

4. How do you target a specific frame from a hyperlink?

Include the name of the frame in the target attribute of the hyperlink. <a href=”mypage.htm” target=”myframe”>>My Page</a>

5. How is JavaScript different from Java?

JavaScript was developed by Brendan Eich of Netscape; Java was developed at Sun Microsystems. While the two languages share some common syntax, they were developed independently of each other and for different audiences. Java is a full-fledged programming language tailored for network computing; it includes hundreds of its own objects, including objects for creating user interfaces that appear in Java applets (in Web browsers) or standalone Java applications. In contrast, JavaScript relies on whatever environment it's operating in for the user interface, such as a Web document's form elements. JavaScript was initially called LiveScript at Netscape while it was under development. A licensing deal between Netscape and Sun at the last minute let Netscape plug the "Java" name into the name of its scripting language. Programmers use entirely different tools for Java and JavaScript. It is also not uncommon for a programmer of one language to be ignorant of the other. The two languages don't rely on each other and are intended for different purposes. In some ways, the "Java" name on JavaScript has confused the world's understanding of the differences between the two. On the other hand, JavaScript is much easier to learn than Java and can offer a gentle introduction for newcomers who want to graduate to Java and the kinds of applications you can develop with it.

6. How Page Re-direction works ?

This is very simple to do a page redirect using JavaScript at client side.

To redirect your site visitors to a new page, you just need to add a line in your head section as follows: 

<head> 

<script type="text/javascript"> 

<!-- window.location="http://www.newlocation.com";//-->

</script>

</head>

7. How to access an external javascript file that is stored externally and not embedded?

This can be achieved by using the following tag between head tags or between body tags. 

<script src="abc.js">

</script>

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.

8. How to comment javascript code?

Use 

// for line comments

and 

/* */ for block comments

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

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

10. 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.
 However , this can be done by coding a Java applet that reads files for the script.

11. How to set the cursor to wait ?

In theory, we should cache the current state of the cursor and then put it back to its original state. document.body.

style.cursor = 'wait';

//do something interesting and time consuming document.

body.style.cursor = 'auto';

12. How to write a script for "Select" lists using javascript?

1. To remove an item from a list set it to null mySelectObject.options[3] = null; 2. To truncate a list set its length to the maximum size you desire mySelectObject.length = 2; 3. To delete all options in a select object set the length to 0. mySelectObject.leng

13. Name the numeric constants representing max,min values

Number.MAX_VALUE 

Number.MIN_VALUE

14. Taking a developer?s perspective, do you think that that JavaScript is easy to learn and use?

One of the reasons JavaScript has the word "script" in it is that as a programming language, the vocabulary of the core language is compact compared to full-fledged programming languages. If you already program in Java or C, you actually have to unlearn some concepts that had been beaten into you. For example, JavaScript is a loosely typed language, which means that a variable doesn't care if it's holding a string, a number, or a reference to an object; the same variable can even change what type of data it holds while a script runs. The other part of JavaScript implementation in browsers that makes it easier to learn is that most of the objects you script are pre-defined for the author, and they largely represent physical things you can see on a page: a text box, an image, and so on. It's easier to say, "OK, these are the things I'm working with and I'll use scripting to make them do such and such," instead of having to dream up the user interface, conceive of and code objects, and handle the interaction between objects and users. With scripting, you tend to write a _lot_ less code.

15. Text From Your Clipboard?

It is true, text you last copied for pasting (copy & paste) can be stolen when you visit web sites using a combination of JavaScript and ASP (or PHP, or CGI) to write your possible sensitive data to a database on another server.

16. What and where are the best JavaScript resources on the Web?

The Web has several FAQ areas on JavaScript. The best place to start is something called the meta-FAQ [14-Jan-2001 Editor's Note: I can't point to it anymore, it is broken!], which provides a high-level overview of the JavaScript help available on the Net. As for fact-filled FAQs, I recommend one maintained by Martin Webb and a mini-FAQ that I maintain. For interactive help with specific problems, nothing beats the primary JavaScript Usenet newsgroup, comp.lang.javascript. Depending on my work backlog, I answer questions posted there from time to time. Netscape and Microsoft also have vendor-specific developer discussion groups as well as detailed documentation for the scripting and object model implementations.

17. What does isNaN function do?

Return true if the argument is not a number.

18. What does javascript null mean?

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.

19. What does the "Access is Denied" IE error mean?

The "Access Denied" error in any browser is due to the following reason.

 A javascript in one window or frame is tries to access another window or frame whose document's domain is different from the document containing the script.

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

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.

Example of using Regular Expressions for syntax checking in JavaScript ... 

var re = new RegExp("^(&[A-Za-z_0-9]{1,}=[A-Za-z_0-9]{1,})*$");

var text = myWidget.value; var OK = re.test(text); if( ! OK )

{ alert("The extra parameters need some work.\r\n Should be something like: \"&a=1&c=4\"");

}

21. What is JavaScript?

JavaScript is a general-purpose programming language designed to let programmers of all skill levels control the behavior of software objects. The language is used most widely today in Web browsers whose software objects tend to represent a variety of HTML elements in a document and the document itself. But the language can be--and is--used with other kinds of objects in other environments. For example, Adobe Acrobat Forms uses JavaScript as its underlying scripting language to glue together objects that are unique to the forms generated by Adobe Acrobat. Therefore, it is important to distinguish JavaScript, the language, from the objects it can communicate with in any particular environment. When used for Web documents, the scripts go directly inside the HTML documents and are downloaded to the browser with the rest of the HTML tags and content.

Simply:

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

22. What is negative infinity?

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

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

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

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

26. Write a function that generates a web page containing a table with 3 columns and500 rows. Label each location in the table with row and column location?

function genTable()
 {
 document.open();
 document.write("<html><head><title>the
 title</title></head><body><table>");
 for (var i=1; i <= 500; i++)
 {
 document.write("<tr> <td> row" + i + "col 1 </td>");
 document.write("<td> row" + i + "col 2 </td>");
 document.write("<td> row" + i + "col 3 </td></tr>");
 }
 document.write("</table></body></html>");
 document.close();
 }

27. Write a function that keeps prompting the user for input until the letter "p" is typed bythe user?

function iqTest()
 {
 var letterP = prompt("Please enter the letter p"," ");
 while (letterP!=="p")
 {
 letterP = prompt("No dummy! Enter the letter p","");
 }
 alert("Thank you!")
 }

28. Write a function that prompts the user for a color and uses what they typed to set thebackground color of the web page?

<html>
 <head><title>Changing Background Color</title>
 <script language="JavaScript">
 function changeBgColor(){
 var bodycolor = document.form1.body.value;
 document.getElementById("bdy").style.backgroundColor = bodycolor;
 }
 </script>
 </head>
 <body ID="bdy">
 <p>
 Pick a background color for this page.
 </p>
 <form name="form1">
 <b> Color </b>
 <input type="text" name="body"><br>
 <input type="button" value="Change color" onclick="changeBgColor()">
 <br>
 </form>
 </body>
 </html>

29. Write a function that prompts the user to input a number, and then gives them 5chances to guess the square of the number?

<script language="JavaScript">
 var theNumber = prompt("pick a number","");
 var num = parseInt(theNumber);
 var theSquare;
 var theirNumber;
 var guess;
 theSquare= num * num;
 for (var i=1; i <= 5; i++)
 {
 theirNumber= prompt("what is " + num + " squared","0");
 guess= parseInt(theirNumber);
 if (guess == theSquare)
 {
 alert("Correct");
 break;
 } else
 {
 alert("Attempt " + i + " is wrong");
 }
 }
 </script>

30. Write a function that takes 3 parameters, price, tax rate, balance, and prints yes inan alert box if someone has adequate balance to purchase an item at the specifiedprice with the given tax rate. Otherwise, print out no in an alert box?

<script language="JavaScript">
 function affortIt(price, taxrate, balance)
 {
 var total = price + price *taxrate;
 if (total <= balance)
 {
 alert("yes");
 } else
 {
 alert("no");
 }
 }
 </script>

31. Write a function that takes 4 variables, a, b, c, d, and prints true in an alert box ifinput a = input b and input c = input d. Else, print false in an alert box?

<html>
 <head>
 <title>Question I-3</title>
 <script language="JavaScript">
 function question9()
 {
 var a = prompt("Give me an \"a\"","");
 var af = parseFloat(a);
 var b = prompt("Give me an \"b\"","");
 var bf = parseFloat(b);
 var c = prompt("Give me an \"c\"","");
 var cf = parseFloat(c);
 var d = prompt("Give me an \"d\"","");
 var df = parseFloat(d);
 if (af>=bf&&cf<=df) {
 alert("true")
 }
 else {
 alert("false")
 }
 }
 </script>
 </head>
 <body>
 <form>
 <input type="button" value="determine conditional"
 onclick="question9()">
 </form>
 </body>
 </html>

32. Write a function tip(bill) that will calculate and return the amount that should be leftas a tip for a restaurant bill when passed the parameter bill. The tip should be 20%of the total before a tip is added?

<html>
 <head>
 <title>Question 9</title>
 <script language="JavaScript">
 function tip(bill)
 {
 var bill, t;
 t = .2*bill;
 return t;
 }
 </script>
 </head>
 <body>
 <script>
 var a = prompt("how much is the bill?","");
 var af = parseFloat(a);
 t = tip(af);
 alert("Please leave a tip of $" +t)
 </script>
 </body>
 </html>

33. Write the details of a function that prompts the user for a color and uses what theytyped to set the link color within the existing web page?

function changeLinkColor()
 {
 var color=prompt(``enter a color please'','''');
 document.link=color;
 }