JavaScript Interview Questions and Answers (294) - Page 2

What is the result of below given line of code in Java Script? 5+4+'7'

The answer is 97.

As 5 and 4 are integer so total becomes 9 and it's add '7' to it as string so it becomes 97.
What is undefined value means in JavaScript?

There can be multiple reasons of having undefined values
1. Object does not exist. Like you create an object of a control which does not exists in your page and when you access it, it is undefined.
2. No value is assigned to the object.
3. If you are trying to access a property which does not exists for the object.
What is === operator in JavaScript and how it is different from == operator?

=== operator checks for the equality. It gives true when both the compared values are having same value and same data type As === does not perform any type conversion while comparing.

It is different from == as == does the type conversion while comparing.

Thanks,
Virendra Dugar
Can Javascript code be broken in different lines?

Yes,
Breaking is possible within a string statement by using a backslash "\" at the end .
Ex:
document.write("Good Morning. \
I am Mr. John");

But it is not possible within any other javascript statement.
Ex :
is possible but not
document.write \
("Good Morning. I am Mr. John");
How to get the Scroll height and Width in JavaScript !!

Width : window.pageXOffset ||
document.body.scrollLeft ||
document.documentElement.scrollLeft;

Height: window.pageYOffset ||
document.body.scrollTop ||
document.documentElement.scrollTop;
What is undefined variable?

Variables which are declared but not assigned any value is called undefined variable.
Is it possible make a call to server side event of any button using javascript?

Yes, it's possible. You can use __doPostBack() function to call server side event.
What is other equivalent option of document.getElementById() when you are working with Ajax?

The other equivalent option of document.getElementById() is $get() ;.

For example.

var x =  $get('<%= upnlScoping.ClientID %>'); 

var y = document.getElementById('<%= upnlScoping.ClientID %>');

How can you set position of the page (Top and Left) to 0 using Javascript in one line code?

Well, there is an inbuilt function in JavaScript named scroll() which takes 2 arguments x and y.

If you want to set top and left to 0 then call this.

window.scroll(0,0);

Write a way by which you can do something on the close of the window ?

call onUnload on the body tag and write your javascript code there

e.g.

<body onUnload=''alert('thanks for visiting us !!')">

How to create an Object in JavaScript ?

1) var obj = new Object();
2) var ob = {};
Basic methods for opening a PopUp window using Javascript?

There are 2 different simple ways using javascript.

they are
1) Using Window.Open() and
2) Using Window.showModalDialog()

The Syntax for using these methods are

. Window.Open(URL,WindowName,Window Features)

Sample example:
window.open ("http://www.dotnetfunda.com","mywindow","status=1,toolbar=1");

. window.showModalDialog(URL,WindowName,ModalDialog Features)


Sample Example:
window.showModalDialog("http://www.dotnetfunda.com", "mywindow","dialogWidth:400px;dialogHeight:395px");

What is JSON?

JSON stands for JavaScript Object Notation . It is language independent and derived from Java Script. This is a LightWeight data format designed for data interchange over a network. Basically this is used for data serialization and data transmission which helps to transfer data between the server and the web application using JavaScript.
Which object you would use for displaying the running text in the Statusbar?

NOTE: This is objective type question, Please click question title for correct answer.
How can you detect the Client Operating System using Javascript?

just type this command in a .html file

<script>

alert(window.navigator.appVersion);
</script>

What is the data type of variables in JavaScript?

Object is the datatype of javascript variables.

Javascript is a weakly typed language:It does not use int, string, DateTime etc
as the datatype with the variables declaration as we do in c#.

variables are declared using the var keyword which can accept any data.
What is negative infinity?

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

Code snippet
<script>

var a=-45;
alert(a/0);
</script>


output:
-Infinity
if 2 methods have same name and same number of parameters, which one will be executed first?

The second method will be executed first

Code snippet.

<script>

function demo(s)
{
alert("first"+"---"+s);
}
function demo(s)
{
alert("Second"+"--"+s);
}
demo("javascript");
demo("jscript");
demo("jp");
</script>


output:
//In this example, the function with alert("Second"+"---"+s); statement will

//be executed 3 times, while the first function will not execute at all.

Difference between undefined and undeclared variables.

undefined variables are those that are not assigned any value but declared in the program.
if we try to read the value, an error message "undefined" is displayed.


undeclared variables are those that are not declared in the program .
if we try to read their values gives runtime error.
But if undeclared variables are assigned some value then implicit declaration is done .

example:
<script>
//a is undefined variable
var a;
alert(a);
//b is undeclared variable
alert("hello"+"--"+b);
</script>

a:undefined:

b:will also be undefined but that error message can be seen by clicking in browser
status bar: if some value is assigned to b, then it will be displayed in the output
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