JavaScript Interview Questions and Answers
Experienced / Expert level questions & answers
Ques 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.
Ques 2. Write a javascript to get difference between two dates.
now = new Date();
newYear = new Date(2004,0,1);
secs = Math.round((now-newYear)/1000);
days = Math.floor(secs / 86400);
secs = days*86400;
hours = Math.floor(secs / 3600);
secs = hours*3600;
minutes = Math.floor(secs / 60);
secs = minutes*60
document.write(days + " days, " +
hours + " hours, " +
minutes + " minutes, and " +
secs + " seconds.");
Ques 3. What is User-Defined class in Javascript?
can define new classes, but the notation is awkward
simply define a function that serves as a constructor
specify data fields & methods using this
no data hiding: can't protect data or methods
function Die(sides)
{
this.numSides = sides;
this.numRolls = 0;
this.Roll = Roll;
}
function Roll()
{
this.numRolls++;
return Math.floor(Math.random()*this.numSides) + 1;
}
die6 = new Die(6);
die8 = new Die(8);
roll6 = -1; // dummy value to start loop
roll8 = -2; // dummy value to start loop
while (roll6 != roll8) {
roll6 = die6.Roll();
roll8 = die8.Roll();
document.write("6-sided: " + roll6 +
" " +
"8-sided: " + roll8 + "
");
}
Ques 4. 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
Ques 5. Are you concerned that older browsers don't support JavaScript and thus exclude a set of Web users or individual users?
Fragmentation of the installed base of browsers will only get worse. By definition, it can never improve unless absolutely everyone on the planet threw away their old browsers and upgraded to the latest gee-whiz versions. But even then, there are plenty of discrepancies between the scriptability of the latest Netscape Navigator and Microsoft Internet Explorer.
The situation makes scripting a challenge, especially for newcomers who may not be aware of the limitations of earlier browsers. A lot of effort in my books and ancillary material goes toward helping scripters know what features work in which browsers and how to either workaround limitations in earlier browsers or raise the compatibility common denominator.
Designing scripts for a Web site requires making some hard decisions about if, when, and how to implement the advantages scripting offers a page to your audience. For public Web sites, I recommend using scripting in an additive way: let sufficient content stand on its own, but let scriptable browser users receive an enhanced experience, preferably with the same HTML document.
Ques 6. 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.
Ques 7. How to hide javascript code from old browsers that dont run it?
Use the below specified style of comments