- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
 Take a Tour Practice InterviewNew Search by Name or Email Play Games Questions Forum

Exams Attended

Mock Exams

Make Homepage

# JavaScript Interview Questions and Answers

##### Ques 21. What are Math routines in Javascript?

Ans. the Math object contains functions and constants

Math.sqrt
Math.pow
Math.abs
Math.max
Math.min
Math.floor
Math.ceil
Math.round

Math.PI
Math.E

Math.random function returns number in [0..1)

##### Ques 22. Write a javascript to get difference between two dates.
Ans. 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 23. What about Navigator object?
Ans. navigator.appName property that gives the browser name.
navigator.appVersion property that gives the browser version.
navigator.userAgent property that gives the browser related information.
##### Ques 24. What is User-Defined class in Javascript?
Ans. 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 25. Few events in Javascript.
Ans. onsubmit - call when submit button is clicked
onclick - call when this button is clicked
onreset - call when the reset button is clicked