JavaScript Interview Questions and Answers


1.JavaScript Introduction:

JavaScript is the most popular programming language in the world. It is the language for HTML and the web, for servers, PCs, laptops, tablets, smart phones, and more.

2.What is JavaScript?

JavaScript is a Scripting Language.It is a lightweight programming language. JavaScript is programming code that can be inserted into HTML pages and it is executed by all modern web browsers..

3.What are JavaScript types? 

 Number, String, Boolean, Function, Object, Null, Undefined.

4.What is this keyword? 

 It refers to the current object.

5.How do you create a new object in JavaScript? 

var obj = new Object(); or var obj = {};

6.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);


7.What is the importance of <SCRIPT> tag?

JavaScript is used inside <SCRIPT> tag in HTML document. The tags that are provided provides the necessary information like alret to the browser for the program to begin interpreting all the text between the tags. The <script> tag uses JavaScript interpreter to handle the libraries that are written or the code of the program. JavaScript is a case sensitive language and the tags are used to tell the browser that if it is JavaScript enabled to use the text written in between the <Script> and </Script> tags. 

The example is given as:

<HTML>
<HEAD>
<!--
<SCRIPT> // Starting of the scripting language
//Your code here
</SCRIPT> --> // End of the scripting language
</HEAD>
<BODY>
// your code here
</BODY>
</HTML>


8.What are the different objects used in JavaScripts?  

JavaScripts uses a hierarchical structure that applies to the objects in a document. There are some objects that show the relationship of one object to another using the language.

Window object: This is the topmost object in the hierarchy. It represent the content area of browser window that consists of HTML documents. Each frame is also a window that has some actions inside it.


Document object: This object gets loaded in a window and consists of objects of different kind in the model. It consists of the content that will be written in the script.

Form object: Form objects are used for more interaction with the users. It represents the form elements inside <FORM>...</FORM> tag. 

9.What are the methods involved in JavaScript?

Method is an informative that gets performed over an action that is related to the object. Method either performs on some object or affect any part of the the script or a document. Object can have as many number of methods that have associations with other objects.

There is a method that is used by the JavaScript statement that includes a reference to an object this is given as:

document.orderForm.submit()

document.orderForm.entry.select()

These are the functions which perform the dynamic interaction with the user. The first statement execute the element when pressed submit button to send a form to a server. These two statements are related to only the form. The scripts that are invoked will have the write of the document as well and will be written as:

document.write(“Give the version “ + navigator.appVersion)

document.write(“ of <B>” + navigator.appName + “</B>.”)
10.Why is object naming important to use in JavaScript?  

Object naming is used to create script references to objects while assigning names to every scriptable object in the HTML code. The browsers that are script compatible looks for the optional tags and attributes that enables the assigning of a unique name to each object. 
The example is:
 <form name=”dataEntry” method=get>
<input type=”text” name=”entry”>
<frame src=”info.html” name=”main”>


The names act as a nametags through which the elements can be easily identified and easily located by the browsers. The references made for each object includes the object hierarchy from the top down. References are used to include names of each object that are coming in the window object. The object naming conventions are easy way to loacte the objects and the linking between them can be done more comfortably. 


11.What is the difference between == and === ?  

The == checks for value equality, but === checks for both type and value.



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

13.How you define a function in javascript?

A function is a set of statements that will be executed when "someone" calls it.
  
Syntax:

function functionname()
{
some code to be executed
}

14.What are the javascript operators?

Two types of operators are there..
a.Arithmetic Operators:
 Arithmetic operators are used to perform arithmetic operation between variables and/or values.
 Ex:   +,-,*,/,%,++,--
 b.Assignment Operators:
 Assignment operators are used to assign values to variables.
Ex:  =,+=,-=, *=,/=,%=

15.What are the javascript objects?

"Everything" in JavaScript is an Object: a String, a Number, an Array, a Date....
In JavaScript, an object is data, with properties and methods.

Properties are values associated with an object.
Methods are actions that can be performed on objects.

16.How you declare a variable in javascript?

You declare JavaScript variables with the var keyword.
Ex. var name;
 you can also assign a value to the variable when you declare it.
Ex. var name="blog";
17.What are the conditional statements in javascript?
In JavaScript we have the following conditional statements:
  • if statement - use this statement to execute some code only if a specified condition is true
  • if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false
  • if...else if....else statement - use this statement to select one of many blocks of code to be executed
  • switch statement - use this statement to select one of many blocks of code to be executed
 18.Tell me something about javascript validations?
JavaScript can be used to validate data in HTML forms before sending the content to a server.
We have to check the following form data:
  • has the user left required fields empty?
  • has the user entered a valid e-mail address?
  • has the user entered a valid date?
  • has the user entered text in a numeric field?
19.What is the HTML DOM?
DOM means Document Object Model.
When a web page is loaded, the browser creates a Document Object Model of the page.

20.What is browser object model?

The Browser Object Model (BOM) allows JavaScript to "talk to" the browser.
The Window Object:
The window object is supported by all browsers. It represent the browsers window.
All global JavaScript objects, functions, and variables automatically become members of the window object.
Global variables are properties of the window object.
Global functions are methods of the window object.
Even the document object (of the HTML DOM) is a property of the window object:
window.document.getElementById("header");
is the same as:
document.getElementById("header");

21.How can you find the size of a window?

Three different properties can be used to determine the size of the browser window (the browser viewport, NOT including toolbars and scrollbars).
For Internet Explorer, Chrome, Firefox, Opera, and Safari:
  • window.innerHeight - the inner height of the browser window
  • window.innerWidth - the inner width of the browser window
For Internet Explorer 8, 7, 6, 5:
  • document.documentElement.clientHeight
  • document.documentElement.clientWidth (or)
  • document.body.clientHeight
  • document.body.clientWidth
22.What does window.open() method?
Opens a new window.
23.What does window.close() method?
Closes the current window. 
24.What does window.moveTo() method?

Move the current window.

25.What does window.resizeTo() method?

Resize the current window.

26.What is a Cookie? 

A cookie is a variable that is stored on the visitor's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With JavaScript, you can both create and retrieve cookie values.
Examples of cookies:
  • Name cookie - The first time a visitor arrives to your web page, he or she must fill in her/his name. The name is then stored in a cookie. Next time the visitor arrives at your page, he or she could get a welcome message like "Welcome John Doe!" The name is retrieved from the stored cookie
  • Date cookie - The first time a visitor arrives to your web page, the current date is stored in a cookie. Next time the visitor arrives at your page, he or she could get a message like "Your last visit was on Tuesday August 11, 2005!" The date is retrieved from the stored cookie.
27.What are the properties of window screen?

The window.screen object can be written without the window prefix.
  •  screen.availWidth - It gives the available screen width.
  • screen.availHeight - It gives the available screen height.
28.What does window.history object?

The window.history object can be written without the window prefix.
To protect the privacy of the users, there are limitations to how JavaScript can access this object.
Some methods:
  • history.back() - same as clicking back in the browser
  • history.forward() - same as clicking forward in the browser
29.  What does window history back?

The history.back() method loads the previous URL in the history list.

30.What does Window History Forward?

The history forward() method loads the next URL in the history list.

31.What are javascript Timing Events?

It is possible to execute some code at specified time-intervals.This is called timing events
The two key methods that are used are:
  • setInterval() - executes a function, over and over again, at specified time intervals.
  • setTimeout() - executes a function, once, after waiting a specified number of milliseconds.
32.What is the syntax of setInterval() method?

The setInterval() method will wait a specified number of milliseconds, and then execute a specified function, and it will continue to execute the function, once at every given time-interval.
Syntax:
window.setInterval("javascript function",milliseconds);
The window.setInterval() method can be written without the window prefix.
The first parameter of setInterval() should be a function.
The second parameter indicates the length of the time-intervals between each execution.

33.How to Stop the Execution of the function specified in the setInterval() method?

The clearInterval() method is used to stop further executions of the function specified in the setInterval() method.
Syntax:
window.clearInterval(intervalVariable)
The window.clearInterval() method can be written without the window prefix.
To be able to use the clearInterval() method, you must use a global variable when creating the interval method:
myVar=setInterval("javascript function",milliseconds);

34.What is the syntax of setTimeout() method?

The setTimeout() method will wait the specified number of milliseconds, and then execute the specified function.

Syntax:
window.setTimeout("javascript function",milliseconds);
The first parameter of setTimeout() should be a function.
The second parameter indicates how many milliseconds, from now, you want to execute the first parameter.

35.Give an example to setInterval() method?

Example:
Alert "hello" every 3 seconds:
setInterval(function(){alert("Hello")},3000);
36. How to Stop the Execution of the function specified in the setTimeout() method.?

The clearTimeout() method is used to stop the execution of the function specified in the setTimeout() method.
Syntax:
window.clearTimeout(timeoutVariable)
The window.clearTimeout() method can be written without the window prefix.
To be able to use the clearTimeout() method, you must use a global variable when creating the timeout method:
myVar=setTimeout("javascript function",milliseconds);
Then, if the function has not already been executed, you will be able to stop the execution by calling the clearTimeout() method.

37.What does window navigator object?

The window.navigator object can be written without the window prefix.

38.What type of popup boxes are in javascript?

JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box

 Alert box:
An alert box is often used if you want to make sure information comes through to the user.
When an alert box pops up, the user will have to click "OK" to proceed.
Syntax:

window.alert("sometext");
Confirm Box:
A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.
Syntax:
window.confirm("sometext");
Prompt Box:
A prompt box is often used if you want the user to input a value before entering a page.
When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.
Syntax:
window.prompt("sometext","defaultvalue");
39.Define Javascript Prototype?
Prototype provides functions to make HTML DOM programming easier.
Like jQuery, Prototype has its $() function.
The $() function accepts HTML DOM element id values (or DOM elements), and adds new functionality to DOM objects.
Unlike jQuery, Prototype has no ready() method to take the place of window.onload(). Instead, Prototype adds extensions to the browser and the HTML DOM.
In JavaScript, you can assign a function to handle the window’s load event:

The JavaScript Way:

function myFunction()
{
var obj=document.getElementById("h01");
obj.innerHTML="Hello Prototype";
}
onload=myFunction;
The Prototype equivalent is different:

The Prototype Way:

function myFunction()
{
$("h01").insert("Hello Prototype!");
}
Event.observe(window,"load",myFunction);
Event.observe() accepts three arguments:
  • The HTML DOM or BOM (Browser Object Model) object you want to handle
  • The event you want to handle
  • The function you want to call
40.What are javascript frameworks/libraries?

The most popular JavaScript frameworks:
  • jQuery
  • Prototype
  • MooTools
All of these frameworks have functions for common JavaScript tasks like animations, DOM manipulation, and Ajax handling.

41.Describe jQuery framework?

jQuery is the most popular JavaScript frameworks on the Internet today.
It uses CSS selectors to access and manipulate HTML elements (DOM Objects) on a web page.
jQuery also provides a companion UI (user interface) framework and a plug-ins.
Many of the largest companies on the Web uses jQuery:
  • Google
  • Microsoft
  • IBM
  • Netflix
42.Describe Prototype framework?

Prototype is a JavaScript library that provides a simple API to perform common web tasks.
API is short for Application Programming Interface. It is a library of properties and methods for manipulating the HTML DOM.
Prototype enhances JavaScript by providing classes and inheritance.

43.Describe MooTools framework?

MooTools is also a framework that offers an API to make common JavaScript programming easier.
MooTools also includes some lightweight effects and animation functions.

44. Describe YUI framework?

The Yahoo! User Interface Framework is a large library that covers a lot of functions, from simple JavaScript utilities to complete internet widgets. 

45. Describe Ext JS framework?

Customizable widgets for building rich Internet applications.

46.Describe Dojo framework?

A toolkit designed around packages for DOM manipulation, events, widgets, and more.

47.Describe script.aculo.us framework?

Open-source JavaScript framework for visual effects and interface behaviors.

48.What is CDN?

CDN - Content Delivery Networks
You always want your web pages to be as fast as possible. You want to keep the size of your pages as small as possible, and you want the browser to cache as much as possible.
If many different web sites use the same JavaScript framework, it makes sense to host the framework library in a common location for every web page to share.

A CDN (Content Delivery Network) solves this. A CDN is a network of servers containing shared code libraries.
Google provides a free CDN for a number of JavaScript libraries, including:
  • jQuery
  • Prototype
  • MooTools
  • Dojo
  • Yahoo! YUI
49.What try,catch and throw statements does in javascript?
The try statement lets you to test a block of code for errors.
The catch statement lets you handle the error.
The throw statement lets you create custom errors.

50.When onLoad and onunload events are used in javascript?

The onload and onunload events are triggered when the user enters or leaves the page.
The onload event can be used to check the visitor's browser type and browser version, and load the proper version of the web page based on the information.
The onload and onunload events can be used to deal with cookies.



No comments:

Post a Comment