JavaScript Interview Questions

What is JavaScript?

JavaScript is a lightweight, interpreted programming language with object-oriented capabilities that allows you to build interactivity into otherwise static HTML pages.

The general-purpose core of the language has been embedded in Netscape, Internet Explorer, and other web browsers.

Name some of the JavaScript features?

Following are the features of JavaScript −

  • JavaScript is a lightweight, interpreted programming language.
  • JavaScript is designed for creating network-centric applications.
  • JavaScript is complementary to and integrated with Java.
  • JavaScript is is complementary to and integrated with HTML.
  • JavaScript is open and cross-platform.

What are the advantages of using JavaScript?

Following are the advantages of using JavaScript −

  • Less server interaction − You can validate user input before sending the page off to the server. This saves server traffic, which means less load on your server.
  • Immediate feedback to the visitors − They don’t have to wait for a page reload to see if they have forgotten to enter something.
  • Increased interactivity − You can create interfaces that react when the user hovers over them with a mouse or activates them via the keyboard.
  • Richer interfaces − You can use JavaScript to include such items as drag-and-drop components and sliders to give a Rich Interface to your site visitors.

What are disadvantages of using JavaScript?

We can not treat JavaScript as a full fledged programming language. It lacks the following important features −

  • Client-side JavaScript does not allow the reading or writing of files. This has been kept for security reason.
  • JavaScript can not be used for Networking applications because there is no such support available.
  • JavaScript doesn’t have any multithreading or multiprocess capabilities.

Is JavaScript a case-sensitive language?

Yes! JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.

How can you create an Object in JavaScript?

JavaScript supports Object concept very well. You can create an object using the object literal as follows −

var emp = {
   name: "Zara",
   age: 10
};

How can you create an Array in JavaScript?

You can define arrays using the array literal as follows −

var x = [];
var y = [1, 2, 3, 4, 5];

How to read elements of an array in JavaScript?

An array has a length property that is useful for iteration. We can read elements of an array as follows −

var x = [1, 2, 3, 4, 5];
for (var i = 0; i < x.length; i++) {
   // Do something with x[i]
}

How many types of functions JavaScript supports?

A function in JavaScript can be either named or anonymous.

How to define a anonymous function?

An anonymous function can be defined in similar way as a normal function but it would not have any name.

What is arguments object in JavaScript?

JavaScript variable arguments represents the arguments passed to a function.

What is callback?

A callback is a plain JavaScript function passed to some method as an argument or option. Some callbacks are just events, called to give the user a chance to react when a certain state is triggered.

What is closure?

Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope.

Give an example of closure?

Following example shows how the variable counter is visible within the create, increment, and print functions, but not outside of them −

function create() {
   var counter = 0;
   return {
      increment: function() {
         counter++;
      },

      print: function() {
         console.log(counter);
      }
   }
}
var c = create();
c.increment();
c.print();     // ==> 1

Which built-in method returns the character at the specified index?

charAt() method returns the character at the specified index.

What are the variable naming conventions in JavaScript?

While naming your variables in JavaScript keep following rules in mind.

You should not use any of the JavaScript reserved keyword as variable name. These keywords are mentioned in the next section. For example, break or boolean variable names are not valid.

JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or the underscore character. For example, 123test is an invalid variable name but _123test is a valid one.

JavaScript variable names are case sensitive. For example, Name and name are two different variables.

How typeof operator works?

The typeof is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand.

The typeof operator evaluates to “number”, “string”, or “boolean” if its operand is a number, string, or boolean value and returns true or false based on the evaluation.

Can you access Cookie using javascript?

JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookie or cookies that apply to the current web page.

How to create a Cookie using JavaScript?

The simplest way to create a cookie is to assign a string value to the document.cookie object, which looks like this .

Syntax :

document.cookie = "key1 = value1; key2 = value2; expires = date";

Here expires attribute is option. If you provide this attribute with a valid date or time then cookie will expire at the given date or time and after that cookies’ value will not be accessible.

How to delete a Cookie using JavaScript?

Sometimes you will want to delete a cookie so that subsequent attempts to read the cookie return nothing. To do this, you just need to set the expiration date to a time in the past.

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 Data Types?

Following are the JavaScript Data types:

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

What is the use of isNaN function?

isNan function returns true if the argument is not a number otherwise it is false.

Between JavaScript and an ASP script, which is faster?

JavaScript is faster. JavaScript is a client-side language and thus it does not need the assistance of the web server to execute. On the other hand, ASP is a server-side language and hence is always slower than JavaScript.  Javascript now is also a server side language (nodejs).

javascript-code-snippet

Javascript

What is negative infinity?

Negative Infinity is a number in JavaScript which can be derived by dividing negative number by zero.

Is it possible to break JavaScript Code into several lines?

Breaking within a string statement can be done by the use of a backslash, ‘\’, at the end of the first line

Example:

And if you change to a new line when not within a string statement, then javaScript ignores break in line.

Which company developed JavaScript?

Netscape is the software company who developed JavaScript.

What are undeclared and undefined variables?

Undeclared variables are those that do not exist in a program and are not declared. If the program tries to read the value of an undeclared variable, then a runtime error is encountered.

Undefined variables are those that are declared in the program but have not been given any value. If the program tries to read the value of an undefined variable, an undefined value is returned.

Write the code for adding new elements dynamically?

1
2
3
4
5
6
7
8
9
<html>
<head> <title>t1</title>
<script type=“text/javascript”>
function addNode() { var newP = document.createElement(“p”);
var textNode = document.createTextNode(” This is a new text node”);
newP.appendChild(textNode); document.getElementById(“firstP”).appendChild(newP); }
</script> </head>
<body> <p id=“firstP”>firstP<p> </body>
</html>

What are global variables? How are these variable declared and what are the problems associated with using them?

Global variables are those that are available throughout the length of the code, that is, these have no scope. The var keyword is used to declare a local variable or object. If the var keyword is omitted, a global variable is declared.

Example:

// Declare a global globalVariable = “Test”;

The problems that are faced by using global variables are the clash of variable names of local and global scope. Also, it is difficult to debug and test the code that relies on global variables.

What is a prompt box?

A prompt box is a box which allows the user to enter input by providing a text box.  Label and box will be provided to enter the text or number.

What is ‘this’ keyword in JavaScript?

‘ This’ keyword refers to the object from where it was called.

Explain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any?

Timers are used to execute a piece of code at a set time or also to repeat the code in a given interval of time. This is done by using the functions setTimeout, setInterval and clearInterval.

The setTimeout(function, delay) function is used to start a timer that calls a particular function after the mentioned delay. The setInterval(function, delay) function is used to repeatedly execute the given function in the mentioned delay and only halts when cancelled. The clearInterval(id) function instructs the timer to stop.

Timers are operated within a single thread, and thus events might queue up, waiting to be executed.

Which symbol is used for comments in Javascript?

// for Single line comments and

/*   Multi

Line

Comment

*/

What is the difference between ViewState and SessionState?

‘ ViewState’ is specific to a page in a session.

‘SessionState’ is specific to user specific data that can be accessed across all pages in the web application.

What is === operator?

=== is called as strict equality operator which returns true when the two operands are having the same value without any type conversion.

Explain how can you submit a form using JavaScript?

To submit a form using JavaScript use document.form[0].submit();

document.form[0].submit();

Does JavaScript support automatic type conversion?

Yes JavaScript does support automatic type conversion, it is the common way of type conversion used by JavaScript developers.

How can the style/class of an element be changed?

It can be done in the following way:

Explain how to read and write a file using JavaScript?

There are two ways to read and write a file using JavaScript

  • Using JavaScript extensions
  • Using a web page and Active X objects

What are all the looping structures in JavaScript?

Following are looping structures in Javascript:

  • For
  • While
  • do-while loops

What is called Variable typing in Javascript?

Variable typing is used to assign a number to a variable and the same variable can be assigned to a string.

Example

This is called variable typing.

How can you convert the string of any base to integer in JavaScript?

The parseInt() function is used to convert numbers between different bases. parseInt() takes the string to be converted as its first parameter, and the second parameter is the base of the given string.

In order to convert 4F (of base 16) to integer, the code used will be –

Explain the difference between “==” and “===”?

“==” checks only for equality in value whereas “===” is a stricter equality test and returns false if either the value or the type of the two variables are different.

What would be the result of 3+2+”7″?

Since 3 and 2 are integers, they will be added numerically. And since 7 is a string, its concatenation will be done. So the result would be 57.

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

What do mean by NULL in Javascript?

The NULL value is used to represent no value or no object.  It implies no object or null string, no valid boolean value, no number and no array object.

What is the function of delete operator?

The functionality of delete operator is used to delete all variables and objects in a program but it cannot delete variables declared with VAR keyword.

What is an undefined value in JavaScript?

Undefined value means the

  • Variable used in the code doesn’t exist
  • Variable is not assigned to any value
  • Property doesn’t exist

What are all the types of Pop up boxes available in JavaScript?

  • Alert
  • Confirm and
  • Prompt

What is the use of Void(0)?

Void(0) is used to prevent the page from refreshing and parameter “zero” is passed while calling.

Void(0) is used to call another method without refreshing the page.

How can a page be forced to load another page in JavaScript?

The following code has to be inserted to achieve the desired effect:

What is the data type of variables of in JavaScript?

All variables in the JavaScript are object data types.

What is the difference between an alert box and a confirmation box?

An alert box displays only one button which is the OK button.

But a Confirmation box displays two buttons namely OK and cancel.

What are escape characters?

Escape characters (Backslash) is used when working with special characters like single quotes, double quotes, apostrophes and ampersands. Place backslash before the characters to make it display.

Example:

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.
Example:
var cloths = [“Shirt”, “Pant”, “TShirt”];
cloths.pop();
//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:

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

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:

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 –

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:

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

The status can be acquired as follows –

alert(document.getElementById(‘checkbox1’).checked);

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:

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 –

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 –

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.

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.

Is JavaScript case sensitive? Give an example?

Yes, JavaScript is case sensitive. For example, a function parseInt is not same as the function Parseint.

What boolean operators can be used in JavaScript?

The ‘And’ Operator (&&), ‘Or’  Operator (||) and the ‘Not’ Operator (!) can be used in JavaScript.

*Operators are without the parenthesis.

How can a particular frame be targeted, from a hyperlink, in JavaScript?

This can be done by including the name of the required frame in the hyperlink using the ‘target’ attribute.

What is the role of break and continue statements?

Break statement is used to come out of the current loop while the continue statement continues the current loop with a new recurrence.

Write the point of difference between web-garden and a web-farm?

Both web-garden and web-farm are web hosting systems. The only difference is that web-garden is a setup that includes many processors in a single server while web-farm is a larger setup that uses more than one server.

How are object properties assigned?

Assigning properties to objects is done in the same way as a value is assigned to a variable. For example, a form object’s action value is assigned as ‘submit’ in the following manner – Document.form.action=”submit”

What is the method for reading and writing a file in JavaScript?

This can be done by Using JavaScript extensions (runs from JavaScript Editor), example for opening of a file –

How are DOM utilized in JavaScript?

DOM stands for Document Object Model and is responsible for how various objects in a document interact with each other. DOM is required for developing web pages, which includes objects like paragraph, links, etc. These objects can be operated to include actions like add or delete. DOM is also required to add extra capabilities to a web page. On top of that, the use of API gives an advantage over other existing models.

How are event handlers utilized in JavaScript?

Events are the actions that result from activities, such as clicking a link or filling a form, by the user. An event handler is required to manage proper execution of all these events. Event handlers are an extra attribute of the object. This attribute includes event’s name and the action taken if the event takes place.

Explain the role of deferred scripts in JavaScript?

By default, the parsing of the HTML code, during page loading, is paused until the script has not stopped executing. It means, if the server is slow or the script is particularly heavy, then the webpage is displayed with a delay. While using Deferred, scripts delays execution of the script till the time HTML parser is running. This reduces the loading time of web pages and they get displayed faster.

What are the various functional components in JavaScript?

The different functional components in JavaScript are-

First-class functions: Functions in JavaScript are utilized as first class objects. This usually means that these functions can be passed as arguments to other functions, returned as values from other functions, assigned to variables or can also be stored in data structures.

Nested functions: The functions, which are defined inside other functions, are called Nested functions. They are called ‘everytime’ the main function is invoked.

Write about the errors shown in JavaScript?

JavaScript gives a message if it encounters an error. The recognized errors are –

  • Load-time errors: The errors shown at the time of the page loading are counted under Load-time errors. These errors are encountered by the use of improper syntax, and thus are detected while the page is getting loaded.
  • Run-time errors: This is the error that comes up while the program is running. It is caused by illegal operations, for example, division of a number by zero, or trying to access a non-existent area of the memory.
  • Logic errors: It is caused by the use of syntactically correct code, which does not fulfill the required task. For example, an infinite loop.

What are Screen objects?

Screen objects are used to read the information from the client’s screen. The properties of screen objects are –

  • AvailHeight: Gives the height of client’s screen
  • AvailWidth: Gives the width of client’s screen.
  • ColorDepth: Gives the bit depth of images on the client’s screen
  • Height: Gives the total height of the client’s screen, including the taskbar
  • Width: Gives the total width of the client’s screen, including the taskbar

Explain the unshift() method ?

This method is functional at the starting of the array, unlike the push(). It adds the desired number of elements to the top of an array. For example –

The output is shown below:

Summary
Review Date
Reviewed Item
Very Nice Java Script Interview Questions and Answers very helpful for freshers, Kindly keep updating more.
Author Rating
51star1star1star1star1star