Most Asked JavaScript Interview Questions and Answers

JavaScript Interview Questions and Answers most commonly asked for Experienced PDF, Freshers candidates for Employment.

What are JavaScript Cookies?

Cookies are the small test files stored in a computer and it gets created when the user visits the websites to store information that they need. Example could be User Name details and shopping cart information from the previous visits.

Explain what is pop()method in JavaScript?

The pop() method is similar as the shift() method but the difference is that the Shift method works at the start of the array.  Also the pop() method take the last element off of the given array and returns it. The array on which is called is then altered.
var cloths = [“Shirt”, “Pant”, “TShirt”];
//Now cloth becomes Shirt,Pant

Whether JavaScript has concept level scope?

No. JavaScript does not have concept level scope. The variable declared inside the function has scope inside the function.

Mention what is the disadvantage of using innerHTML in JavaScript?

If you use innerHTML in JavaScript the disadvantage is

  • Content is replaced everywhere

  • We cannot use like “appending to innerHTML”

  • Even if you use +=like “innerHTML = innerHTML + ‘html’” still the old content is replaced by html

  • The entire innerHTML content is re-parsed and build into elements, therefore its much slower

  • The innerHTML does not provide validation and therefore we can potentially insert valid and broken HTML in the document and break it

What is break and continue statements?

Break statement exits from the current loop.

Continue statement continues with next statement of the loop.

What are the two basic groups of dataypes in JavaScript?

They are as –

  • Primitive

  • Reference types.

Primitive types are number and Boolean data types. Reference types are more complex types like strings and dates.

How generic objects can be created?

Generic objects can be created as:

var I = new object();

What is the use of type of operator?

‘Typeof’ is an operator which is used to return a string description of the type of a variable.

Which keywords are used to handle exceptions?

Try… Catch—finally is used to handle exceptions in the JavaScript





Code to throw an exception



Code runs either it finishes successfully or after catch


Which keyword is used to print the text in the screen?

document.write(“Welcome”) is used to print the text – Welcome in the screen.

What is the use of blur function?

Blur function is used to remove the focus from the specified object.

What is variable typing?

Variable typing is used to assign a number to a variable and then assign string to the same variable. Example is as follows:

i= 8;


How to find operating system in the client machine using JavaScript?

The ‘Navigator.appversion’ is used to find the name of the operating system in the client machine.

What are the different types of errors in JavaScript?

There are three types of errors:

  • Load time errors: Errors which come up when loading a web page like improper syntax errors are known as Load time errors and it generates the errors dynamically.

  • Run time errors: Errors that come due to misuse of the command inside the HTML language.

  • Logical Errors: These are the errors that occur due to the bad logic performed on a function which is having different operation.

What is the use of Push method in JavaScript?

The push method is used to add or append one or more elements to the end of an Array. Using this method, we can append multiple elements by passing multiple arguments

What is unshift method in JavaScript?

Unshift method is like push method which works at the beginning of the array.  This method is used to prepend one or more elements to the beginning of the array.

What is the difference between JavaScript and Jscript?

Both are almost similar. JavaScript is developed by Netscape and Jscript was developed by Microsoft .

How are object properties assigned?

Properties are assigned to objects in the following way

obj[“class”] = 12;


obj.class = 12;

What is the ‘Strict’ mode in JavaScript and how can it be enabled?

Strict Mode adds certain compulsions to JavaScript. Under the strict mode, JavaScript shows errors for a piece of codes, which did not show an error before, but might be problematic and potentially unsafe. Strict mode also solves some mistakes that hamper the JavaScript engines to work efficiently.

Strict mode can be enabled by adding the string literal “use strict” above the file. This can be illustrated by the given example:

function myfunction()


“use strict”;

var v = “This is a strict mode function”;


What is the way to get the status of a CheckBox?

The status can be acquired as follows –


If the CheckBox will be checked, this alert will return TRUE.

How can the OS of the client machine be detected?

The navigator.appVersion string can be used to detect the operating system on the client machine.

Explain window.onload and onDocumentReady?

The onload function is not run until all the information on the page is loaded. This leads to a substantial delay before any code is executed.

onDocumentReady loads the code just after the DOM is loaded. This allows early manipulation of the code.

How will you explain closures in JavaScript? When are they used?

Closure is a locally declared variable related to a function which stays in memory when the function has returned.

For example:

function greet(message) {



function greeter(name, age) {

return name + ” says howdy!! He is ” + age + ” years old”;


// Generate the message

var message = greeter(“James”, 23);

// Pass it explicitly to greet


This function can be better represented by using closures

function greeter(name, age) {

var message = name + ” says howdy!! He is ” + age + ” years old”;

return function greet() {




// Generate the closure

var JamesGreeter = greeter(“James”, 23);

// Use the closure


How can a value be appended to an array?

A value can be appended to an array in the given manner –

arr[arr.length] = value;

Explain the for-in loop?

The for-in loop is used to loop through the properties of an object.

The syntax for the for-in loop is

for (variable name in object){

statement or block to execute


In each repetition, one property from the object is associated to the variable name, and the loop is continued till all the properties of the object are depleted.

Describe the properties of an anonymous function in JavaScript?

A function that is declared without any named identifier is known as an anonymous function. In general, an anonymous function is inaccessible after its declaration.

Anonymous function declaration

var anon = function() {

alert(‘I am anonymous’);



What is the difference between .call() and .apply()?

The function .call() and .apply() are very similar in their usage except a little difference. .call() is used when the number of the function’s arguments are known to the programmer, as they have to be mentioned as arguments in the call statement. On the other hand, .apply() is used when the number is not known. The function .apply() expects the argument to be an array.

The basic difference between .call() and .apply() is in the way arguments are passed to the function. Their usage can be illustrated by the given example.

var someObject = {

myProperty : ‘Foo’,

myMethod : function(prefix, postfix) {

alert(prefix + this.myProperty + postfix);



someObject.myMethod(‘<‘, ‘>’); // alerts ‘<Foo>’

var someOtherObject  = {

myProperty : ‘Bar’

};, ‘<‘, ‘>’); // alerts ‘<Bar>’

someObject.myMethod.apply(someOtherObject, [‘<‘, ‘>’]); // alerts ‘<Bar>’

Define event bubbling?

JavaScript allows DOM elements to be nested inside each other. In such a case, if the handler of the child is clicked, the handler of parent will also work as if it were clicked too.


Popular posts from this blog

TOP Agile Testing Interview Questions and Answers

Latest Agile Testing Interview Questions and Answers

Most Asked ADO.NET Interview Questions and Answers