Javascript Interview Questions and Answers


In this section, I have selected advanced javascript interview questions and answers for Freshers/Experienced Candidates.Last few months I have been working to select best question and answer set for JavaScript interview questions.I have written some  JavaScript  tutorials.

What are JavaScript types? 
Number, String, Boolean, Function, Object, Null, Undefined.

How do you convert numbers between different bases in JavaScript?
Use the parseInt() function, that takes a string as the first parameter, and the base as a second parameter. So to convert hexadecimal 3F to decimal, use parseInt ("3F", 16);

What does "1"+2+3 evaluate to?
Since 1 is a string, everything is a string, so the result is 123.

How about 3+5+"8"?
Since 3 and 5 are integers, this is number arithmetic, since 8 is a string, it’s concatenation, so 88 is the result.

How do you submit a form using Javascript? 
Use document.forms[0].submit();

How do you assign object properties?
obj["age"] = 22 or obj.age = 22.

What’s a way to append a value to an array?
arr[arr.length] = value;

What does isNaN function do? 
Return true if the argument is not a number.

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.

How to read and write a file using javascript? 
I/O operations like reading or writing a file is not possible with client-side javascript.

How do you convert numbers between different bases in JavaScript? 
Use the parseInt() function, that takes a string as the first parameter, and the base as a second parameter. So to convert hexadecimal FF to decimal, use parseInt ("FF", 16);

What is negative infinity? 
It’s a number in JavaScript, derived by dividing negative number by zero.

How to set a HTML document's background color? 
document.bgcolor property can be set to any appropriate color.

What boolean operators does JavaScript support?
&&, and !

How to get the contents of an input box using Javascript? 
Use the "value" property.
var myValue = window.document.getElementById("textboxID").value;

How to determine the state of a checkbox using Javascript? 
var checkedP = window.document.getElementById("CheckBoxID").checked;

How to set the focus in an element using Javascript? 
<script> function setFocus() { if(focusElement != null) { document.forms[0].elements["myelementname"].focus(); } } </script>

How to access an external javascript file that is stored externally and not embedded? 
This can be achieved by using the following tag between head tags or between body tags.
<script src="raj.js"></script>How to access an external javascript file that is stored externally and not embedded? where abc.js is the external javscript file to be accessed.

What is the difference between an alert box and a confirmation box? 
An alert box displays only one button which is the OK button whereas the Confirm box displays two buttons namely OK and cancel.

What is a prompt box? 
A prompt box allows the user to enter input by providing a text box.

Can javascript code be broken in different lines? 
Breaking is possible within a string statement by using a backslash \ at the end but not within any other javascript statement.
that is ,
document.write("Hello \ world");
is possible but not document.write \
("hello world");

What looping structures are there in JavaScript?
for, while, do-while loops, but no foreach.

How do you create a new object in JavaScript?
var obj = new Object(); or var obj = {};

What is this keyword?
It refers to the current object.


What is the difference between SessionState and ViewState? 
ViewState is specific to a page in a session. Session state refers to user specific data that can be accessed across all pages in the web application.


What looping structures are there in JavaScript? 
for, while, do-while loops, but no foreach.

To put a "close window" link on a page ? 
<a href='javascript:window.close()' class='mainnav'> Close </a>

How to hide javascript code from old browsers that dont run it? 
Use the below specified style of comments <script language=javascript> <!-- javascript code goes here // --> or Use the <NOSCRIPT>some html code </NOSCRIPT> tags and code the display html statements between these and this will appear on the page if the browser does not support javascript

How to comment javascript code? 
Use // for line comments and
/*
*/ for block comments

Name the numeric constants representing max,min values 
Number.MAX_VALUE
Number.MIN_VALUE

What does javascript null mean? 
The null value is a unique value representing no value or no object.
It implies no object,or null string,no valid boolean value,no number and no array object.

How do you create a new object in JavaScript? 
var obj = new Object(); or var obj = {};

How do you assign object properties? 
obj["age"] = 23 or obj.age = 23.

What’s a way to append a value to an array? 
arr[arr.length] = value;


To set all checkboxes to true using JavaScript? 
//select all input tags
function SelectAll() {
var checkboxes = document.getElementsByTagName("input");
for(i=0;i<checkboxes.length;i++) {
if(checkboxes.item(i).attributes["type"].value == "checkbox") {
checkboxes.item(i).checked = true;
}
}
}

What does undefined value mean 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 undefined value and null value? 
(i)Undefined value cannot be explicitly stated that is there is no keyword called undefined whereas null value has keyword called null
(ii)typeof undefined variable or property returns undefined whereas typeof null value returns object

What is variable typing in javascript? 
It is perfectly legal to assign a number to a variable and then assign a string to the same variable as follows
example
i = 10;
i = "string";
This is called variable typing

Does javascript have the concept level scope? 
No. JavaScript does not have block level scope, all the variables declared inside a function possess the same level of scope unlike c,c++,java.

What are undefined and undeclared variables? 
Undeclared variables are those that are not declared in the program (do not exist at all),trying to read their values gives runtime error.But if undeclared variables are assigned then implicit declaration is done .
Undefined variables are those that are not assigned any value but are declared in the program.Trying to read such variables gives special value called undefined value.

What is === operator ? 
==== is strict equality operator ,it returns true only when the two operands are having the same value without any type conversion.


How to disable an HTML object ?
document.getElementById("myObject").disabled = true;

How to create a popup warning box?
alert('Warning: Please enter an integer between 0 and 1000.');

How to create a confirmation box? 
confirm("Do you really want to launch the missile?");

How to create an input box? 
prompt("What is your temperature?");

How to force a page to go to another page using JavaScript ? 
<script language="JavaScript" type="text/javascript" ><!-- location.href="http://rajeshstutorials.blogspt.com"; //--></script>

What's Math Constants and Functions using JavaScript? 
The Math object contains useful constants such as Math.PI, Math.E

Math.abs(value); //absolute value
Math.max(value1, value2); //find the largest
Math.random() //generate a decimal number between 0 and 1
Math.floor(Math.random()*101) //generate a decimal number between 0 and 100

What does the delete operator do? 
The delete operator is used to delete all the variables and objects used in the program ,but it does not delete variables declared with var keyword.

How to get value from a textbox?
alert(document.getElementById('txtbox1').value);

How to get value from dropdown (select) control?
alert(document.getElementById('dropdown1').value);