JavaScript Interview Questions and Answers (294) - Page 8

How to hide Javascript code from browsers?

With the help of <NOSCRIPT> tag,we can hide any code:-

<NOSCRIPT>write some code here</NOSCRIPT>

What is undefined value in Javascript?

Undefined value means the variable used in the code doesn't exist or is not assigned any value or the property doesn't exist.
What is the difference between below statement? var func = function() {} function func() {}

var func = function() {} -> Defines a variable that references an anonymous function.

function func() {} -> Defines a named function "func".
What will be the output of below statement? alert(NaN == NaN);

It will give us the output as false because NaN is not equal to any value,including NaN.
Does Javascript string.toLowerCase() follow Unicode standards in case-conversions?

Suppose,below is my Javascript function:-

function Is_Upper(value)

{
if(value != value.toLowerCase())
{
return True;
}
else
{
return False;
}
}


Yes,above function will work.Actually,the Boolean keywords are true and false i.e. in lower case.
How to get textbox character' count in Javascript?

With the help of Length property of textbox control,we can count number of characters:-

<asp:TextBox ID="TextBox1" runat="server" Text="Rajesh Kumar"></asp:TextBox>


var count = document.getElementById('<%=TextBox1.ClientID %>').value.Length;
alert(count);


Output:12
How to call JavaScript function on button click?

We can use OnClientClick event of Button control for raising any Javascript function.

For Example:-
function alert_msg()

{
alert('rajesh');
}

<asp:button id="btn_alert" OnClientClick="alert_msg();"/>

What is the alternate way of calling Javascript function instead of OnClientClick event of Button control?

We can use onClick event of Button control for calling any Javascript function.

onClick event is same as OnClientClick event.

For Example:-
function alert_msg()

{
alert('rajesh');
}

<asp:button id="btn_alert" onClick ="alert_msg();"/>

How to avoid page refreshing/postback in calling Javascript function?

Just write return false semicolon after Javascript function in Button OnClientClick event as:-

<asp:Button ID="btn_alert" runat="server" Text="Get Alert" OnClientClick="alert_msg(); return false;" />


function alert_msg()
{
alert('Rajesh Kumar');
}

What is the alternate way of avoiding Postback on click on button when calling javascript function instead of writing return false in onclientclick event?

Use OnSubmit Event of Form tag to avoid postback:

<form name="frm" onsubmit = "return js_function()">

<input type="submit" name="Submit" value="Get Alert"/>
</form>


Here,js_function() is my Javascript function,and write return false inside that Javascript function.
How to declare/define any variable in JavaScipt?

Use var to declare or define any variable in Javascript.

For Example:

function define_variable()

{
var id = 0;
var name = 'Rajesh Kumar';
var isenabled = true;
}

How to refresh any page in JavaScript?

With the help of location.reload() Javascript method,we can refresh any page in Javascript.

function Refresh_Page()

{
location.reload();
}

How many ways of Refreshing the page?

NOTE: This is objective type question, Please click question title for correct answer.
What is the use of Math function in Javascript?

Math function is used for handling Maths related operation like calculating Square Roots of any value,PI,Trigonometry functions like Sin,Cos,Tan and so on.It's in-built class in Javascript and same as Dot Net Math class.

For Example:-
var PI_value = Math.PI;


Output:PI means 22/7=3.14
How to change Textbox Color in Javascript?

With the help of style'color property of Textbox,we can change any color.

For Example:
document.getElementById("TextBox1").style.color = "red";


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

Alternate way of changing Textbox color other than writing Javascript function?

We can write in-line css in textbox definition for changing Textbox color like below code:

<asp:TextBox ID="TextBox2" runat="server" style="color:red;" ></asp:TextBox>

What is the output of below Javascript code alert(Math.sqrt(4));

Output will be 2.Actually SQRT method evaluates square root of any value same a mathematics square root.

So, square root of 4 will be 2.

Take another example as

square root of 16 will be 4.
Explain few Methods of Math Class?

Math class has all static methods like,some of listed below:
Sqrt: Used for calculating Square Root of any value.
Round: Used for Rounding value to the nearest integer.
Random: Used for populating Random values.
Tan: Returns the Tangent of an angle.
Cos: Returns the Cosine of an angle
Sin: Returns the Sine of an angle
Pow(x,y): Returns the value of x to the power of y.
What does Math.Ceil method do in Javascript?

Ceil is an static method of Math class.It rounds a number upward to it's nearest integer.
In other word,we can say that it returns the smallest integer greater than or equal to a number.
Note:- If we pass parameter as an integer no decimal,the value will not be rounded.

Syntax:
Math.ceil(any_numeric_or_decimal_value);

Give an example of Math.Ceil method?

Below are the example of math.Ceil method:

Math.ceil(.80);
//Output would be 1
Math.ceil(10); 
//Output would be 10
Math.ceil(9.45);
//Output would be 10
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