Home » » Javascript Interview Questions and Answers

Javascript Interview Questions and Answers

Written By 1 on Monday, June 3, 2013 | 6:35 AM


JavaScript is a general-purpose programming language designed to let programmers of all skill levels control the behavior of software objects. The language is used most widely today in Web browsers whose software objects tend to represent a variety of HTML elements in a document and the document itself. You can change the DOM element and call the Ajax. It is independent of operating and language.


Question: How many types of loop are there in javaScript?

Answer: JavaScript supports following different types of loops
for - loops through a block of code a number of times
for/in - loops through the properties of an object
while - loops through a block of code while a specified condition is true
do/while - also loops through a block of code while a specified condition is true
/** for loop example **/
cars=["BMW","Volvo","Merchi","Ford"];
var i=2,len=cars.length;
for (; i");
}
/** for loop example **/

/** For/In Loop **/
var person={fname:"John",lname:"Doe",age:25};

for (x in person)
{
alert(person[x]);
}
/** For/In Loop **/

/** while loop **/
while (i<5 data-blogger-escaped-br="" data-blogger-escaped-he="" data-blogger-escaped-i="" data-blogger-escaped-is="" data-blogger-escaped-number="" data-blogger-escaped-x="x">";
i++;
}
/** while loop **/




Question:What are global variables? How are they declared? How these are different from local variables?
Answer: Variable that are available throughout the page.
These are declared without use of var keyword.

Variable that are declared with use of keyword var are local variable and available within scope.
// Declare a local variable
var localVariable = "PHP Tutorial"
// Declare a global
globalVariable = "google"


Question:What is the difference between undefined and null?
Answer: The value of a variable with no value is undefined (i.e., it has not been initialized). Variables can be emptied by setting their value to null. You can use === operator to test this.


Question: What are the various datatypes in javascript?
Answer: Number - Store Number like 1,100, 33
String - Store string like "php","tutorial-" etc
Boolean - Store true OR false
Function - Store the function
Object - Store the object like person object
Null - Store the variable value null
Undefined - Not defined variable value are undefined.

Question: What is negative infinity?
Answer: It’s a number in JavaScript, derived by dividing negative number by zero. For example var a=-100/0;

Question: How to check the type of variable?
Answertypeof is used to check the variable type. For example alert(typeof abc);


Question: How do you convert numbers between different bases in JavaScript?
Answer: Use the parseInt() function.
alert( parseInt ("3F", 16));


Question: What is Javascript namespacing? How and where is it used?
Answer: Using global variables in Javascript is evil and a bad practice. That being said, namespacing is used to bundle up all your functionality using a unique name. In JavaScript, a namespace is really just an object that you’ve attached all further methods, properties and objects.

Question: How to load javascript files asynchronously?
Question: How to load javascript files fast?
 Answer: If you have latest browser which support HTML5 then you just need to add "async" tag with value true
If you have old browser, you need to create a js function that will add javascript async Following are the Javascript function example
function loadScriptAsync (scriptFilePath){
var scriptHeadTag=document.getElementsByTagName('script')[0];
var ss=document.createElement('script');
ss.type='text/javascript';
ss.async=true;
ss.src= scriptFilePath
scriptHeadTag.parentNode.insertBefore(ss,s);
}

loadScriptAsync('/js/jsfile.js');

0 Comment:

Post a Comment