JavaScript Interview Questions and Answers (294) - Page 9

What will be the output of below Javascript code:- var output = Math.ceil(-30.02); alert(output);

Output would be: -30

As we know that,Ceil method returns the smallest integer or value and here -30 is a smallest numeric value,so it will omit decimal part and return only -30.
What will happen if we pass string value in Ceil method? Will it throw any error or not.

If we pass any string value in Ceil Method,then it will not throw any error but will show NAN means Not A Number as output.

For Example:

alert(Math.ceil("Rajesh"));


Output:NAN
What is the use of Random method in Javascript?

The Random() function is an static method of Math class which returns a floating-point number between 0 and 1.It has no parameter defined.

Note: Every time Random function will produce new number when it is called.

Syntax:
Math.random()

How to get only random number between 1 to 10?

Just multiply with 10,then we can get output between 1 to 10 as

alert(Math.floor((Math.random() * 10)));


Output would be any value between 1 to 10.
How to get only random number between 1 and 100?

Just multiply with 100,then we can get output between 1 to 100 as

alert(Math.floor((Math.random() * 100)));


Output would be any value between 1 and 100.
Explain properties available in Math class?

Following are the properties available in Math Class:-

PI Returns PI(i.e. 22/7 approx. 3.14)
E Returns Euler's number(approx. 2.718)
LN2 Returns the natural logarithm of 2(approx. 0.693)
LN10 Returns the natural logarithm of 10(approx. 2.302)
LOG2E Returns the base-2 logarithm of E(approx. 1.442)
LOG10E Returns the base-10 logarithm of E(approx. 0.434)
SQRT1_2 Returns the square root of 1/2(approx. 0.707)
SQRT2 Returns the square root of 2(approx. 1.414)
What does Math.Floor function do?

Floor is an static method of Math class.The Math.floor() function returns the largest integer less than or equal to a number.
In other words,we can say that,it rounds a number downward to its nearest integer.

Note: If we pass only integer value(no decimal value),then the value will not be rounded.

Syntax:-
Math.floor(any_numeric_decimal_number);

Give some example of Math.Floor method?

Math.Floor method rounds the lowest value to its nearest integer.

Below are some example of Math.Floor method as:

Math.floor(50.07); //Output would be 50 and it will omit decimal portion
Math.floor(-50.07); //Output would be -51
Math.floor(50);//As there is no decimal part provided,then Output would be integer part as 50
What will be the output of below Javascript code,if we pass string value in Floor method? try { alert(Math.floor("Rajesh12.90")); } catch (ex) { alert(ex.message); }

Output would be: NAN

Because If we pass any string value in Floor Method,then it will not throw any error but will show NAN means Not A Number as output.
What is SQRT2 in Math class?

Actually SQRT2 is Math property which returns only the square root of 2 which in Mathematics have approx. 1.414.

So if we want square root of 2,then we can use SQRT2 property.
Because SQRT2 is a static property of Math,we always use it as Math.SQRT2.
What do we mean by SQRT1_2 in Math class?

SQRT1_2 is also an static property of Math class which returns the square root of 1/2.

The square root of 1/2 in Mathematics is approximately 0.707.
What does Math.Max function do?

The Math.max() function returns the largest of zero or more numbers.Max is also an static method of Math,we always use it as Math.max().

In other way,we can say that,Math.max() returns the number with the highest value.

Syntax:
Math.max([value1,value2, ...valuen]]);

What will be the output of below max Javascript function? var output1 = Math.max(55,60); var output2 = Math.max(-15,-90); var output3 = Math.max(10,20,30);

Output of output1 variable will be 60
Output of output2 variable will be -15.
Output of output2 variable will be 30.
Can we pass any value other than numeric in Max function?

No ,we can pass only integer or numeric value in Max function.If we provide value other than numeric,then Max function will return NAN means Not A Number .

For Example:
var value = Math.max("a",20,"b");

If we do not provide any value inside max function,then what will be the output?

If we do not provide any value inside max function,then it will return Infinity meaning that output will be Infinity.

For Example:
var result = Math.max();

What do we mean by Min function of Math class?

The Math.min() function returns the smallest of zero or more numbers.Or we can say that,it return the number with the smallest value.
Min is also an static method of Math,we always use it as Math.min().

Syntax:
Math.min(value1,value2,...,valueN);
Example:
var result1 = Math.min(15,25);
//Output:15
var result2 = Math.min(0,1,2,3);
//Output:0
var result3 = Math.min(1.1,2.2);
//Output:1.1
What is the difference between Math.SQRT2 and Math.sqrt()?

Math.SQRT2 is a Math property where Math.sqrt() is Method available in Math class.

We can calculate Square Root of any value with the help of Math.sqrt() method but we can only get only Square Root of 2 in case of Math.SQRT2 property.
What is Math.Pow function in Math class?

The Math.Pow function returns the base to the exponent Power or we can say that,The pow() method returns the value of x to the power of y.It's same as Mathematics power.

Pow is also an static method of Math,we always use it as Math.pow().

Syntax:-
Math.pow(base,exponent);

What will be the output of below Javascript function? var result = Math.pow(8,2);

Output would be 64.

As it's 8 to the power of 2 meaning 8*8 = 64.

Because pow() method returns the value of base to the power of exponent.

Here, base = 8 and exponent = 2.
What do mean by History object in Javascript?

The History object is used in Javascript to track history of the URLs which are visited by users.It is part of the window object and is accessed through the window.history property.

The history property has the return value as history object,which is an array of history items having details of the URLs visited from within that window.The window.history object can be written without the window prefix.

Syntax:
window.history; 

Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories