Top JavaScript Interview Questions With Answers

JavaScript one of the most popular programming language among web developers. Its a broswer based coding for client side operations. For fast form validations.PHP , Dotnet, java like programming languages are used fro server side coding. Javascript can handle many operations like form field validations, file operations, send requests to server using Ajax, Curl like methods etc. Here I have collected 20 Javascript interview questions that will be asked commonly in interviews. On the last page I have added a PDF version for quick reference. I have just added the questions only and you should prepare the answers before attending the interviews.

JavaScript

JavaScript is dynamic, high-level, untyped and interpreted language used in programming on the web. It is popular and powerful programming language on the web. JavaScript is used by the majority of websites and it is supported by modern browsers without having to make use of plug-ins.

It is prototype-based, with first class functions, which makes it a multi-paradigm language able to support imperative, object-oriented and functional styles of programming. It is standardized in the ECMAScript language specification.

JavaScript interview questions with answers

Here are a few JavaScript interview questions you are likely to come across and the answers to the questions:

  1. What are JavaScript types?

Ans:

Here are the JavaScript types

  • Number
  • Object
  • String
  • Boolean
  • Function
  • Null
  • Undefined
  1. What is negative infinity?

Ans:

Negative infinity is a number which you can get in JavaScript by dividing negative number by zero.

  1. What is the difference between ViewState and SessionState?

Ans:

ViewState is specific to a page in session.

SessionState relates to user specific data which one can access across all pages in the web application.

  1. Does JavaScript support automatic type conversion?

Ans:

Yes. JavaScript supports automatic type conversion. It is mostly used by JavaScript developers as their common way of type conversion.

 

  1. What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block?

Ans:

It is a common practice which many famous JavaScript libraries (jQuery, Node.js, etc) make use of. The technique produces a closing around the whole file’s content, which generates a private namespace, thereby assisting in preventing clash of name between different JavaScript modules and libraries.

 

  1. What would be the output of the code below?

console.log(0.1+0.2);

console.log(0.1+0.2==0.3);

Ans:

An informed answer to this question would rightly be: “You can’t be sure. It is possible to print out ‘0.3’ and ‘true’, or it might just not. In JavaScript, numbers are treated with floating point precision, therefore, may not always give you the result you expect”.

The above example is classic case which demonstrates this issue. Shockingly, this is what it will print out:

0.30000000000000004

false

  1. Write a simple function (less than 80 characters) that returns a Boolean indicating whether or not a string is a palindrome.

Ans:

The following one line will return ‘true’ if str is a palindrome; otherwise, it returns false.

function isPalindrome(str) {

str = str.replace(/\w/g, ”) .toLowerCase();

return (str == str.split(”) .reverse() .join(”));

}

For instance:

console.log(isPalindrome(“level”));                                // logs ‘true’

console.log(ispalindrome(“level”));                                // logs ‘false’

console.log(isPalindrome(“A car, a man, a maraca”));  // logs ‘true’

 

  1. What will the following code output to the console? Please, explain your answer.

console.log((function f(n){return ((n > 1) ? n * f(n-1) : n)})(10));

Ans:

The code will output the value of 10 factorial (i.e., 10!, or 3,628,800).

This is the reason:

The named function f() recursively calls itsefl, until it gets down to calling f(1) which then returns 1. So, here is what this does:

f(1): returns n, which is 1

f(2): returns 2 * f(1), which is 2

f(3): returns 3 * f(2), which is 6

f(4): returns 4 * f(3), which is 24

f(5): returns 5 * f(4), which is 120

f(6): returns 6 * f(5), which is 720

f(7): returns 7 * f(6), which is 5040

f(8): returns 8 * f(7), which is 40320

f(9): returns 9 * f(8), which is 362880

f(10): returns 10 * f(9), which is 3628800

  1. What do you mean by NULL in JavaScript?

Ans:

NULL represents no value or no object. It simply means no object or null string, no array object, no number and no valid Boolean value.

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

Ans:

They include

  • Alert
  • Confirm, and
  • Prompt
  1. You can expect some basic questions like what is Javascript and its difference form other languages like Java
    2_What is JavaScript
  2. 3_difference between JavaScript and java
  3. 4_difference between JavaScript and jscript
  4. Prepare the array operations with Javascript
    5_How to create arrays in JavaScript
  5. 6_What are JavaScript types
  6. 7_How to call external JavaScript file
  7. 8_What is BOM and DOM
  8. 9_What is negative infinity
  9. It will be a sure question about file operations in Javascript
    10_How to read and write a file using javascript

More References / Tutorials