Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Commonly Asked JavaScript Interview Questions with Answers

1. What is JavaScript?

JavaScript is a platform-independent,event-driven, interpreted client-side scripting and

programming language developed by Netscape Communications Corp. and Sun Microsystems.

2.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.
However , this can be done by coding a Java applet that reads files for the script.

3.How to detect the operating system on the client machine?

In order to detect the operating system on the client machine, the navigator.appVersion

string (property) should be used. 




4.Where are cookies actually stored on the hard disk?

This depends on the user's browser and OS.
In the case of Netscape with Windows OS,all the cookies are stored in a single file called

cookies.txt

c:\Program Files\Netscape\Users\username\cookies.txt

In the case of IE,each cookie is stored in a separate file namely username@website.txt.

c:\Windows\Cookies\username@Website.txt

5.What can javascript programs do?

Generation of HTML pages on-the-fly without accessing the Web server.
The user can be given control over the browser like User input validation
Simple computations can be performed on the client's machine
The user's browser, OS, screen size, etc. can be detected
Date and Time Handling

6.How to set a HTML document's background color?

document.bgcolor property can be set to any appropriate color.

7.What does the "Access is Denied" IE error mean?
The "Access Denied" error in any browser is due to the following reason.
A javascript in one window or frame is tries to access another window or frame whose

document's domain is different from the document containing the script.

8.Is a javascript script faster than an ASP script?
Yes.Since javascript is a client-side script it does require the web server's help for its

computation,so it is always faster than any server-side script like ASP,PHP,etc..

9.Are Java and JavaScript the Same?
No.java and javascript are two different languages.
Java is a powerful object - oriented programming language like C++,C whereas Javascript is a

client-side scripting language with some limitations.

10.How to embed javascript in a web page?
javascript code can be embedded in a web page between <script

langugage="javascript"></script> tags

11.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="abc.js"></script>
where abc.js is the external javscript file to be accessed.

12.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.

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

14.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");

14.How to hide javascript code from old browsers that dont run it?
Ans.
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

15. How to comment javascript code?
Ans.
Use // for line comments and
/*

*/ for block comments

16.Name the numeric constants representing max,min values
Ans.

Number.MAX_VALUE
Number.MIN_VALUE

17.What does javascript null mean?

Ans.

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.

18.What does undefined value mean in javascript?

Ans.

Undefined value means the variable used in the code doesnt exist or is not assigned any value or the property doesnt exist.


19.What is the difference between undefined value and null value?

Ans.

(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

20.What is variable typing in javascript?

Ans.

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

21.Does javascript have the concept level scope?

Ans.

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.

22. What are undefined and undeclared variables?

Ans.

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.

23. What is === operator ?

Ans.

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


24.What does the delete operator do?

Ans.

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.

25.What does break and continue statements do?

Ans.

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

26.How to create a function using function constructor?

Ans.

The following example illustrates this
It creates a function called square with argument x and returns x multiplied by itself.

var square = new Function ("x","return x*x");



Javascript Interview Questions with Answers


1. Difference between window.onload and onDocumentReady?
The onload event does not fire until every last piece of the page is loaded, this includes css and images, which means there’s a huge delay before any code is executed.
That isnt what we want. We just want to wait until the DOM is loaded and is able to be manipulated. onDocumentReady allows the programmer to do that.

2. What is the difference between == and === ?
The == checks for value equality, but === checks for both type and value.
3. What does “1″+2+4 evaluate to? What about 5 + 4 + “3″?
Since 1 is a string, everything is a string, so the result is 124. In the second case, its 93.
4. What is the difference between undefined value and null value?
undefined means a variable has been declared but has not yet been assigned a value. On the other hand, null is an assignment value. It can be assigned to a variable as a representation of no value.
Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.
Unassigned variables are initialized by JavaScript with a default value of undefined. JavaScript never sets a value to null. That must be done programmatically.

5. How do you change the style/class on any element?
document.getElementById(“myText”).style.fontSize = “20″;
-or-
document.getElementById(“myText”).className = “anyclass”;

6. What are Javascript closures?When would you use them?
Two one sentence summaries:
* a closure is the local variables for a function – kept alive after the function has returned, or
* a closure is a stack-frame which is not deallocated when the function returns.

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.
The following code returns a reference to a function:
function sayHello2(name) {
var text = ‘Hello ‘ + name; // local variable
var sayAlert = function() { alert(text); }
return sayAlert;
}

Closures reduce the need to pass state around the application. The inner function has access to the variables in the outer function so there is no need to store the information somewhere that the inner function can get it.
This is important when the inner function will be called after the outer function has exited. The most common example of this is when the inner function is being used to handle an event. In this case you get no control over the arguments that are passed to the function so using a closure to keep track of state can be very convenient.
7. What is unobtrusive javascript? How to add behavior to an element using javascript?
Unobtrusive Javascript refers to the argument that the purpose of markup is to describe a document’s structure, not its programmatic behavior and that combining the two negatively impacts a site’s maintainability. Inline event handlers are harder to use and maintain, when one needs to set several events on a single element or when one is using event delegation.
1<input type="text" name="date" />
Say an input field with the name “date” had to be validated at runtime:
1document.getElementsByName("date")[0].
2                   addEventListener("change", validateDate,false);
3
4function validateDate(){
5// Do something when the content of the 'input' element with the name 'date' is changed.
6}
Although there are some browser inconsistencies with the above code, so programmers usually go with a javascript library such as JQuery or YUI to attach behavior to an element like above.
8.  What is Javascript namespacing? How and where is it used?
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. It promotes modularity and code reuse in the application.
9.  What datatypes are supported in Javascript?
Number, String, Undefined, null, Boolean

10. What is the 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


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email