Javascript Questions


javaScript Interview Questions :-

  What is javaScript?

JavaScript is a client-side as well as server side scripting language that can be inserted into HTML pages and is understood by web browsers. JavaScript is also an Object Oriented Programming language


  Enumerate the differences between Java and JavaScript?

Java is a complete programming language. In contrast, JavaScript is a coded program that can be introduced to HTML pages. These two languages are not at all inter-dependent and are designed for the different intent.

Java is an object - oriented programming (OOPS) or structured programming language like C++ or C whereas JavaScript is a client-side scripting language and it is said to be unstructured programming.


  What are JavaScript types?

Following are the JavaScript types:

1. Number

2. String

3. Boolean

4. Function

5. Object

6. Null

7. Undefined


  What are the different actions that are performed using JavaScript?

1. JavaScript allows the computer to be more secure by putting the privacy policies and disable the unauthorized access to the files.

2. It allows many actions to be performed like, setting the browser's preferences and receiving the request from the servers for the client computer.

3. The settings that are used gets saved on the client side on the features like actions buttons, appearance and printing.

4. It allows easy launching of an application on the client computer with interactive data to be communicated between the server and the client.

5. It allows reading and writing of files or directories on the client or server side.

6. It allows easy capturing of the data that is live streamed from the server to the client machine for retransmission.

7. It also allows to safe and secure the application from the outside world.


  How can JavaScript language be separated from objects?

JavaScript treats and creates the applications for the scripting to make the browser's compatible for use. The language is separated from the objects as it allows the syntax to change the environment.

It is a language that keeps the page element in the HTML document. JavaScript allows the elements of the page to remain in sync with the document objects.

The language is used to create objects that are connected to page elements and other elements in a language. The separation allows the concept of development and effort to be shared with each factor.

The JavaScript language allows dynamic data to be presented using the weakly typed language. It also support any action to be taken to support user interface and graphics.


  What is the use of DOM?

DOM is also known as Document Object Model which is used to develop a model with documents or web pages containing objects like elements, links, etc.

These objects can be manipulated or certain actions like add, delete or change of an element can be performed using this document object model. Through this change in attributes can be done to get all the list of all the elements in the document.

The DOM model responds to API calls that result in documented level of DOM recommendation.

It is used to support additional behavior on the web page and use of API give an extensible advantage over other models existing. DOM codes are reused to meet the requirement of the real world and to make all the program interoperable.


  Why JavaScript is called as Script for all browsers?

JavaScript is written after <SCRIPT> tag and it is surrounded in between the <!-- your code --> tags, this is also known as comment tag. JavaScript interpreter treats the tag such that it treats all the lines in the comments as script lines.

The JavaScript comment starts with tag. The script is contained inside <HTML> tag that contains a comment tag.

The browser that is non-compatible with JavaScripts ignore the lines and move on, but compatible browsers always treats it as a script and execute it.

The browser treat the written lines between the comment tag as normal lines and just thinking of the comment ignores it. Some browsers just treat the >!-- comment--> as a comment only and ignores whatever is written inside it.


  What are the requirements of Web application using JavaScript?

There are lots of application that require certain things when the user uses a JavaScript like:

Data entry validation : This tell that if the field of the form is filled out then during the processing of the server the client side can interact with it.

Serverless CGIs : This describes the processes that are not used with JavaScript but programmed as CGI on server, it gives low performace due to more interaction between the applicatioin and the user.

Dynamic HTML interactivity : It allows dynamic position of the data without using any other scripting language.

CGI prototyping : It allows more reduction of time to access the user interface before implementing the application of the CGI.


  What are the different objects used in JavaScripts?

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


  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.


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


  What are the main functions performed by JavaScript Statements?

The <SCRIPT> Script statement(s) </SCRIPT> tag consists of JavaScript statement. Just to be compatible with all the browsers it is been written in comments and it also accepts a semicolon(;) at the end of every statement. JavaScript also uses the carriage return at the end of a statement. Every statement has a purpose like:

1. Define or initialize a variable that will be used in an object when executing the script

2. Assign a value to a property or variable so that user inputs can be taken according to the values that are defined in it.

3. Change the value of a property or variable to give more flexibility to the use of a scripting language.

4. Invoke an object's method to allow faster processing of the system

5. Invoke a function routine to allow the function to perform various activities so that the application can run at faster speed.


  What is the function of Deferred scripts?

Deferred scripts are the scripts whose statements run as grouped together statements as a function. Function provides a definition of a block that a script statement consists of to run the statements that are in the <SCRIPT> tags after all the statements loaded in the browser.

Functions allow to see the user clearly the visiblity inside the <SCRIPT> tag. In this tag each function starts with function() and function name is written after it including the parentheses.

Once a function is loaded it becomes ready to run whenever there is a use of it. It is useful when a function gets used immediately after a page load.

The Window object uses an event handler that triggers the response to the user actions and the handler is onLoad.


  What is the difference between Scripting and Programming?

- Scripting is easier to write than programming. As programming requires more hands on to the code and a language specification to write.

- Scripting consists of lots of tools to easily create an object model and run it using any browser, wheras programming doesn't have many tools to create an object model and it is not easy to use browser compatibility.

- Scripts work with more than just objects, each statement of the JavaScript does something means perform some actions, whereas programming becomes different as each and every action takes time to execute.

- Scriptting doesn't require lots of knowledge to be provided with and can be easily learnt, but to learn a programming language it requires lots of knowledge.


  What does dynamic component in JavaScript contain?

Dynamic component is a model that consists of many other sub-components that are based on dynamic theory only like:

1. Dynamic typing : These are associated with the values and not having any association with the variables. JavaScript support the use of many types that can be used as an object.

2. Object based : JavaScript is an object-oriented language and it deals with the objects that are associated arrays used with prototypes. The properties of the object based component can be modified, removed or added at run-time. The properties are using built in functions that helps to keep the property of dynamicity of the application.

3. Run-time evaluation : JavaScript provides a run time evaluation using the eval() that takes some arguments provided during run time. This evaluation is used to make the scripting language more widely used as run time evaluation slows down the system but JavaScript uses the libraries to take less space.


  What are the different functional component in JavaScript?

The different components that are required in the functional model is :

1. First-class functions : Fuctions that are first class will have some objects whose properties and methods are given by using the methods like length() and call(). These methods can be given different varibles and passed by an argumnent.

2. Nested functions : These are the functions that are used in another function. It is created each time the outer function is invoked. If a function is used inside another then any constants, local variables and argument values, become part of the function object.

3. Closures : JavaScript allows the function to be created with nested functionality. It usually try to find the combination of code that can be used to execute the application.


  What is the difference between script type and the script language attributes?

- The script type specifies the type of content that is used to show the language used by the browsers like "language=text/javascript". This defines the MIME type that is also known as Multipurpose Internet Mail Extensions (MIME). Text in this defines a plain text format and script defines the language that will be used.

- The script language attribute on the other hand specify a particular version of JavaScript language that is required to run the script and to provide a mechanism to fall back if any browser doesn't support it.

- Script type is used to define the type through which the browser can understand the language and all the compatible browsers can execute the type according to themselves, whereas the script language attribute defines the content and its attributes that are used.


  What are the different types of errors supported by JavaScript?

There are many errors that can occur in a system. JavaScript provides a message for all the errors at run time and dynamically generate the error code. The errors that are present :

Load-time errors : These are the errors that come when loading a web page like using of improper syntax and the errors gets detected the moment user loads a page

Run-time errors : These are the errors that comes due to misuse of the language commands used inside the HTML document. This shows that it has loaded but the functionality is not properly integrated.

Logic errors : These are the errors that comes due to bad logic performed on a function that is having different operation but it is made to have logic differently.


  What are Math Constants and Functions using JavaScript?

- Math object has two constant : Math.PI and Math.E

- Math object has following functions :

Math.abs(val1); :It will give absolute value of val1.

Math.max(val1,val2) ; :This fuction will return maximum value from val1 and val2.

Math.random() ; :This function will return a random number between 0 and 1.

Math.floor(val1) ; :This function will returns decimal value of val1.


  Can you explain about Screen object?

- The screen object can be used to retrieve the information about the visitor's screen.

- There are following properties of Screen objects :

avalHeight : This property returns the height of the screen excluding the windows taskbar.

availWidth : This property returns the width of the screen excluding the windows taskbar.

colorDepth : This property returns the bit depth of the color palette to display images.

height : This property returns the total height of the screen.

pixelDepth : This property returns the color resolution of the screen in bits per pixel.

width : This property returns the total width of the screen.


  What is the disadvantages using innerHTML in JavaScript?

- If you use innerHTML the content is replaced everytime.

- We cannot use like 'appending to innerHTML'.

- Even if we use += like "innerHTML = innerHTML + 'html'" then also the old content is replaced by html.

- If we use innerHTML then the entire innerHTML content is re-parsed and build into elements. Therefore it's much slower.

- The innerHTML doesn't provide validation and therefore we can potentially insert invalid and broken HTML in the document and break it.


  What is Strict Mode in JavaScript?

- The fifth edition of ECMAScript specification has introduced Script Mode.

- Strict Mode applies a layer of constraint on JavaScript.

- It throws errors for actions that are problematic and didn't throw an error before.

- It throws errors for the actions which are potentially unsafe.

- It shut downs the functions which are poorly thought out.

- Strict mode solves mistakes that are responsible for the difficulty of JavaScript engines to perform optimizations.

- To enable the strict mode we have to add the string literal "use strict" on the top of file or function as follow :

function myfunction()
{
"use strict";
var v = "I am a strict mode function";
}


  What is the difference between window.onload and onDocumentReady?

- The difference is that onDocumentReady is called after the DOM is loaded without waiting for all the contents to get loaded. While window.onload() function waits until the contents of page is loaded.

- Suppose there is very large image on a page, at that time onDocumentReady will wait until that image is loaded totally.

- So while using the window.onlaod() function the execution will be slow, but the onDocumentReady will not wait until the image is loaded.


  How can I set up my own JavaScript error handler?

To set up your own JavaScript error handler some optional parameters has to be known. These parameters are as follows:

- Textual description of error

- Address (URL) of page on which error occurred

- Number of line in which error occurred
If you want to invoke the default error handler of the browser, then your function should return (false) or vice versa.
Example code:
function handlerFunction(description,page,line)
{
return true;}
window.onerror=handlerFunction;


Hello!

Let's do interview preparation for node.js..

Read more