Sunday, 22 June 2014

JavaScript Interview Questions for both Freshers and 3 + years of Experienced Programmers

JavaScript Interview Questions for both Fresher's and 3 + years of Experienced Programmers



What is JavaScript? Ans:JavaScript is a scripting language most often used for client-side web development.

Is JavaScript case sensitive? Ans:Yes!
A function getElementById is not the same as getElementbyID.

What are the types used in JavaScript? Ans:String, Number, Boolean, Function, Object, Null, Undefined.
 
How do we add JavaScript onto a web page? Ans:There are several way for adding JavaScript on a web page, but there are two ways which are commonly used by developers
If your script code is very short and only for single page, then following ways are the best:
a) You can place <script type="text/javascript"> tag inside the <head> element.
Code
<head>
<title>Page Title</title>
<script language="JavaScript" type="text/javascript">
   var name = "rajul konkar"
   alert(name);
</script>
</head>
b) If your script code is very large, then you can make a JavaScript file and add its path in the following way:
Code
<head>
<title>Page Title</title>
<script type="text/javascript" src="myjavascript.js"></script>
</head>

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

What is the difference between JavaScript and Jscript?
Ans:Both JavaScript and Jscript are almost similar. JavaScript was developed by Netscape. Microsoft reverse engineered Javascript and called it JScript.

How to access the value of a textbox using JavaScript? Ans: ex:-
Code
<!DOCTYPE html>
<html>
<body>
Full name: <input type="text" id="txtName"
name="FirstName" value="rajul">
</body>
</html>
There are following ways to access the value of the above textbox:
var name = document.getElementById('txtName').value;
alert(name);
or:
we can use the old way:
document.forms[0].mybutton.
var name = document.forms[0].FirstName.value;
alert(name);
Note: This uses the "name" attribute of the element to locate it.

What do you understand by this keyword in JavaScript? Ans: In JavaScript the this is a context-pointer and not an object pointer. It gives you the top-most context that is placed on the stack.
The following gives two different results (in the browser, where by-default the window object is the 0-level context):

var obj = { outerWidth : 20 };
function test() {
    alert(this.outerWidth);
}
test();//will alert window.outerWidth
test.apply(obj);//will alert obj.outerWidth

What looping structures are there in JavaScript? Ans: for, while, do-while loops

What are the boolean operators supported by JavaScript?
And Operator: &&
Or Operator: ||
Not Operator: !

What does "9+2+8 evaluate to? Ans: Since 9 is a string, everything is a string, so the result is 928.

What is the difference between “==” and “===”? Ans:
“==” checks equality only,
“===” checks for equality as well as the type.

Does JavaScript Support automatic type conversion, If yes give example.Ans: Yes! Javascript support automatic type conversion.
Ex.
var s = '12';
var a = s*19
var b = +s;
typeof(s); //"string"
typeof(a); //"number"
typeof(b); //"number"
How will you get the Checkbox status whether it is checked or not? Ans:
var status = document.getElementById('checkbox1').checked;
alert(status);
will return true or false.
Name any two JavaScript functions which are used to convert nonnumeric values into numbers? Ans:
Number()
parseInt()
parseFloat()
Code
var s1 = Number(“Welcome to India!”); //NaN
var s2 = Number(“”);             //0
var s3 = Number(“000080”);       //80
var s4 = Number(true);           //1
var s5= Number(NaN);            //NaN
What does 2+3+"4" evaluate to?
Ans: Since 2 and 3 are integers, this is number arithmetic, since 4 is a string, it is concatenation, so 54 is the result.
How you will add function as a property in a JavaScript object? Give an example. Ans:
Code
var saw = new Object();
saw.name = 'swapneel Waghmare';
saw.living = true;
saw.age = 27;
saw.getName = function() { return saw.name;}
console.log(saw.getName()); // Logs 'Swapneel Waghmare'.

What does isNaN function do? Ans: It returns true if the argument is not a number.
Example:
Code
document.write(isNaN("test")+ "<br>");
document.write(isNaN("2014/09/12")+ "<br>");
document.write(isNaN(982)+ "<br>");
The output will be:
true
true
false
How do you change the style/class on any element using JavaScript? Ans:
Code
document.getElementById(“sawText”).style.fontSize = “25";
-or-
document.getElementById(“sawText”).className = “testclass”;

What is the use of Math Object in JavaScript?
Ans: The math object provides you properties and methods for mathematical constants and functions.
ex:-
Code
var x = Math.PI; // Returns PI
var y = Math.sqrt(49); // Returns the square root of 49
var z = Math.sin(90);    Returns the sine of 90
How to create arrays in JavaScript?
Ans:There are two ways to create array in JavaScript like other languages:
a) The first way to create array
Declare Array:
Code
var names = new Array();
Add Elements in Array:-
names[0] = "rajul";
names[1] = "prabhakar";
names[2] = "konkar";
b) This is the second way:
var names = new Array("rajul", "prabhakar", "konkar");

 

No comments:

Post a Comment