JavaScript Interview Questions

JavaScript Interview Questions




What is the difference between == and === ?
The == checks for value equality, but === checks for both type and value.

difference between innerHTML and append() in JavaScript?
InnerHTML is not standard, and its a String. The DOM is not, and although innerHTML is faster and less verbose, its better to use the DOM methods like appendChild(), firstChild.nodeValue, etc to alter innerHTML content.

break and continue statements?
Continue statement continues the current loop (if label not specified) in a new iteration whereas break statement exits the current loop. 



Javascript closures?
A closure takes place when a function creates an environment that binds local variables to it in such a way that they are kept alive after the function has returned. A closure is a special kind of object that combines two things: a function, and any local variables that were in-scope at the time that the closure was created.

What is Strict Mode in JavaScript?
Strict Mode has been introduced as part of ECMAScript 5 and introduces new, restricted variant of JavaScript which has following aims:
  • Throws errors for actions that are rather silly but previously didn’t throw an error
  • Throws errors for potentially unsafe actions
  • Disables functions that are poorly thought out
  • Potentially code in strict mode could run faster by eliminating mistakes that would make it difficult for JavaScript engines to perform optimizations
Is javascript case sensitive?
Yes javascript is case sensitive.
Examples are isNaN , Number, new Array().

How to set the cursor to wait ?
document.body.style.cursor = 'wait';
//do something interesting and time consuming
document.body.style.cursor = 'auto';  



What's Prototypes for JavaScript?  
Objects have "prototypes" from which they may inherit fields and functions. 

 a checkbox using Javascript?  
var checked = window.document.getElementById("thmyCheckBox").checked;
What does "5"+2+4 evaluate to?  
Since 5 is a string, everything is a string, so the result is 524.  
How to create arrays in JavaScript? 
 declare an array like this
var scripts = new Array();
We can add elements to this array like this

scripts[0] = "A";
scripts[1] = "B";
scripts[2] = "C";
scripts[3] = "D"; 
 


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


GET and POST in HTML forms?
GETmethod: Parameters are passed in the querystring. Maximum amount of data that can be sent via the GET method is limited to about 2kb.
POST
method: Parameters are passed in the request body. There is no limit to the amount of data that can be transferred using POST. However, there are limits on the maximum amount of data that can be transferred in one name/value pair.
 


Differentiate between “var a=5” and “a =5” ?
difference is between the two is that one variable is local and the other is global.


How to get value from a textbox?

document.getElementById('txtbox1').value;

What are global variables?

Global variables are available throughout your code: That is, the variables have no scope. Local variables scope, on the other hand, is restricted to where it is declared (like within a function). The var keyword is used to declare a local variable or object, while omitting the var keyword creates a global variable.

// Declare a local variable
var localVariable = "testtest";
// Declare a global
globalVariable = "Cghdg";

What are JavaScript types?

• Number
• String
• Boolean
• Function
• Object
• Null
• Undefined

What is this keyword?

It refers to the current object.


how html Elements using javascript? 
the getElementById method is preferred.
document.getElementById("test").style.color = "green";