JavaScript Interview Questions

JavaScript (JS) is a lightweight, interpreted, or just-in-time compiled programming language with first-class functions. While it is most well-known as the scripting language for Web pages, many non-browser environments also use it, such as Node.js, Apache CouchDB and Adobe Acrobat.

Due to its popularity and usage, there are lot of openings for people having expertise in Javascript. Hence it is important to prepare & read through popular Javascript interview questions. These questions will help you gain and also test your expertise in this area.

Below are the top 100 Javascript interview questions to help you prepare for the interview :

Q 1: Why do we even need JavaScript if we have Java?

Ans – JavaScript is a client-oriented language where Java is specific to object oriented. JavaScript can also be used with HTML. JavaScript will be used specific to client and mostly deal with actions performed in browser. With Java we can handle server side. Java need compilation to at server to do anything.

 

Q 2: How do you find out the type of a variable?

Ans – There is a unary operator called typeOf which tells about the type of a variable. This is very helpful because sometimes we need to first find out the type of a variable before doing any comparison or any operation.

 

Q 3: Does Javascript do automatic type conversion? If yes, give an example. For no, give the reason.

Ans – Yes, Javascript supports automatic type conversion. It goes out of its way to accept every problem which is given to it. For example.

Console.log(100*null);

//Returns – 0;

Console.log(“20” -1);

//Returns – 19;

 

Q 4: What is control execution? Does JavaScript support control execution?

Ans – Control execution is the process of executing the statements from top to bottom where it is controlled with conditions. JavaScript also supports the control execution like Java. It has the same keywords like if, for and while.

 

Q 5: When should we use do-while loop instead of while?

Ans – “while” and “do-while” loop both servs the same purpose but do-while loop checks the condition after executing the inner code once. Being said, if there is a requirement to run the code at least once without checking the condition then “do-while” is the best.

 

Q 6: Explain about self-executing functions?

Ans – JavaScript support self-executing functions. These functions are executed by itself. They are executed just after it’s definition.

(fucntion(){

Console.log(“this is a self-executing function.”);})()

 

 

Q 7: How can javaScript help to remove any element from a website?

Ans – JavaScript has 2 methods remove() and removeChild(). Both of these can be used to remove the HTML element from a website. Remove method removes the HTML element whereas removeChild returns the deleted element.

 

Q 8: How session state is different than view state?

Ans – Session state is an option which is common across all pages where as view state is used for accessing a session only. For different sessions view State will be different and session state can be same.

 

Q 9: What are escape characters? How are they used in JavaScript?

Ans – Sometimes user has to work with special characters for specific requirement. These are double quotes, single quotes or ampersands. For such cases escape characters are used in JavaScript. These characters can be displayed with a backslash.

 

Q 10: How to describe arrow functions?

Ans – Arrow (=>) function are also behaves like other function. Only different is with the definition. The arrow functions are written with an arrow in between parameter and implementations.

For example

Var arrowFunction = () => {return true;};

 

Q 11: What is the concept of optional arguments?

Ans – Java script is a broad-minded language and it doesn’t care much about number of arguments passed. If any method accepts only one argument but user calls it with more than one so java script will ignore other arguments. These parameters are called optional arguments.

 

Q 12: What is confirmation box?

Ans – Confirmation box a feature of java script. If we call confirm method then it will show a confirmation pop up with two buttons. As per user response it communicates the selection back to the parent page.

 

Q 13: What is closures in Javascript?

Ans – The ability to treat functions as values, combined with the fact that local bindings are re-created every time a function is called closure.  Advance feature of this is to bind a particualr value of local binding.

 

 

Q 14: If parameters are not known for any function then how can we define it?

Ans – There is an interesting feature called “Rest Parameters”. Whenever there are undefined number of parameters then REST Parameters can be used. It is defined with three dots (…).

Example – function max (…numbers)

 

Q 15: Is it possible to get the server on client machine?

Ans – Yes, this can be done with JavaScript. This can be done with navigator.app string. User just need to call it at right place to get it. Sometimes we need to define the host name for IP address to make navigator to work.

 

Q 16: What is higher order functions?

Ans – Generally creating a big function is not a good idea because there are more chances errors. Also big functions are difficult to understand. To avoid such issues, bigger functions can be divided into smaller one and one outer function can call other small functions. The bigger function which calls all these are known as higher order functions.

 

Q 17: What are the ways to define variables in javascript? Is it only var?

Ans – As per latest version of JavaScript, variables can be defined with three types.

  1. Var (This is the old way)
  2. Const – This is introduced recently
  3. Let – This one was introduced after angular 2 and typescript

 

Q 18: What is alert box? How is it useful?

Ans – As the name suggest, it shows alert to the user with any message. It only contains one button. This is used only for informational or warning purpose. This is very useful to show confirmed warning. Even developers use it for debugging as well.

 

Q 19: What is is NaN and how is it useful?

Ans – NaN is known as Not a Number. As we already know that JavaScript doesn’t have a lot of data types and it is hard to differentiate between a string and a number. This method can be used to validate if a particular variable is number of not. This will avoid any kind of error which a operation can cause on string or any other datatype.

 

Q 20: What is Encapsulation and does JavaScript support it?

Ans – JavaScript also follows Object Oriented programming concepts and Encapsulation is one of them. As per it, program should be divided into smaller pieces and each piece should be responsible for single thing. Others don’t need to worry about how it is happening. This is called encapsulaion.

 

Q 21: What is the concept of storage?

Ans – Storage deals with storing the information. There are 2 types of storage.

  1. Local – This storage keeps the data until it is cleared manualy .
  2. Session – This storage keeps the data until browser is closed.

 

Q 22: Is It possible to declare an infinite negative number in javaScript?

Ans – Yes, JavaScript supports negative infinite number and it can be declared with a variable name and assign any value to it and divide by zero. This is helpful for comparisons.

 

 

Q 23: What is strict mode and how to enable it?

Ans – Although javaScript a language which ignores common errors done developer and put something from its own side. For example, if developer forgot to put var in front of a variable then it will assume by itself. Assuming can be dangerous sometimes, so there is a mode which can throw error for such cases, called strict mode. It can be enabled by adding “use strict” on top of the js file.

 

Q 24: How “===” is different than “==” ?

Ans –  “==” compares only value on the other side “===” compares both value on type of the variable. It is very helpful sometimes when we have a string comparison with number.

 

Q 25: What is prompt box in javaScript?

Ans – This is also one of the main features of javaScript. This gives an oportunity to provide any input back to the parent web. For example – If there is a requirement when user clicks on a button it asks for user name. Prompt can be used for this case.

 

Q 26: What is a callback and why we need it?

Ans – Callbacks are the functions which are provided as input for slow methods where a result from one method is input for another method. AS javascript don’t wait for one function to finish execution and it starts another one. To handle such issues callback is used.

 

Q 27: As there is no string datatype, is it possible to convert a string to an integer in JavaScript?

Ans – Yes, it is feasible to convert a String to an Integer in javaScript. This can be achieved with pasreInt() method. It takes String as input and also expect string base as second input.

 

Q 28: How JavaScript is different than Angular?

Ans – JavaScript is a language whereas Angular is a framework. JavaScript can work independently without Angular but vice versa is not possible.

 

Q 29: What is failure in javaScript?

Ans – This is one of the features which is linked to asynchronous calls. Sometimes asynchronous methods can throw an error or exception that can be caught with failure method. Failure is not only a code error it can be related to a network or authentication error.

 

Q 30: What is BOM?

Ans – BOM stands for Browser object model. Using BOM interaction with browser is possible. Default object for browser is a window.

 

 

Q 31: What is “undefined” error and how do we tackle it?

Ans – As the name tells, this is thrown when any variable is used in the code which is not defined or not in the scope of the code. To avoid this error, we need put a undefined check before the variable usage.

 

Q 32: What is JSON.stringify?

Ans – This is used to convert the JSON data into a string format. This is always required before we print or return the object. It sometimes through the exception of nested objects.

 

Q 33: What are cookies and how are they used in javaScript?

Ans – These are the smaller files which are stored at the client. It stores some information which can be used throughout the browser usage. It can be cleared manually by user and by program as well. This can also be written specific to domain which enables it to be secure.

 

Q 34: How can we change style of a HTML tag with javaScript?

Ans – It is also possible to change the style of an element with javaScript. There are multiple ways to do it. It can be done with tagId, tag name or reference. We simply need to get the object and then apply the style class.

For example,

Document.getElementById(“tagName”).style.color=”red”;

 

Q 35: How to handle exceptions in javaScript?

Ans – It can be done in same way as Java with keywords like try, catch and finally. The only difference we have here in javaScript is, no need to mention the Exception type in catch block. It will automatically catch it.

 

Q 36: What is an anonymous function?

Ans – As the name tells it is the function without name. These methods are declared at runtime. This can be used in a place of expression.

 

Q 37: Can a javascript code have comments?

Ans – Yes, comments can also be added to javascript code. It can be added in 2 ways.

  1. Single line comment. (//)
  2. Multi line comment. (/* comment text here*/)

 

Q 38: How to debug javaScript code?

Ans – It can be debug with multiple ways.

  1. Console logs – Developer can put console logs and trace the values of the variables.
  2. Alert – alerts can be added to pop up the values of some fields.
  3. Debugger – add a word debugger and user mozilla or chrome browsers to run it.

 

Q 39: What is history object?

Ans – This object is a default object which itself records the history of the pages move. Using this object we can navigate the user to previous pages. We can also track the application usage from history like how user uses it.

 

Q 40: What is a promise and why we need it?

Ans – Promise is also an advance feature of javascript. This is implemented with Promise class. This is an asynchronous action that may complete at some time and produce a result.

 

Q 41: What are binary operators?

Ans – Binary means two, any operator which is applied on two values are called binary operators. For example, +, – etc. (a+b) is a binary operation.

 

Q 42: Why Indentation of code is required?

Ans – Indentation of code is required although it doesn’t impact on functionality but it increases the readability of the code. Also, it makes more sense to have the code in proper format, everything in single line or with same format will have more chances of errors and it is difficult to understand.

 

Q 43: How can we push some value in an empty object without changing it?

Ans – An Object value can be added in an object by converting it into a String. For example

let emptyObject = {};

console.log(emptyObject.toString()); // → [object Object]

 

Q 44: Write a program where it prints “One” for number divisible by 7, “two” for number divisible by 5 and “OneTwo” for numbers divisible by 35, where the numbers start from 7 and goes till 843.

Ans – Program is as follow.

for (var num=7; num <= 843; num++)

{

if (num % 35 == 0)

console.log(“OneTwo”);

else if (i % 5 == 0)

console.log(“One”);

else if (i % 7 == 0)

console.log(“Two”);

}

 

Q 45: Write a program to print a triangle made up of stars (*).

Ans – Program is as follow.

for (var star=1; star <= 10; star++)

{

var printConetent=”;

for(var printStar=0; printStar<star; printStar++){

printConetent+=”*”;

}

console.log(printConetent);

}

 

Q 46: Write a program which takes a number as an input and prints “Hi” if it is greater than 10 and less than 30, and it prints “Hello” for number less than 80 and more than 64 otherwise print “NA”. Write it with conditional operator.

Ans – Program is as follow.

function numbersFunction(num) {

let message = (num>10 && num <30) ? “Hi”

: ((num<80 && num>64) ? “Hi”:”NA”);

console.log(message);

}

 

Q 47: Write a program to reverse values in an array with only 3 lines.

Ans – Program is as follow.

var arr = [1, 2, 3, 4];

var new_arr = arr.reverse();

console.log(new_arr);

 

Q 48: Write a program to show an infinite for loop.

Ans – Infinite for loop mean the loop which keep on running and there is no break and control statement. Program is as below.

for(;;)

console.log(“infinite loop”);

 

Q 49: Write a program which takes a positive number as input and prints it backwards from the number to 1. Do it with recursion.

Ans – Program is as follow.

function recursion(num){

If(num<1) return;

console.log(“Recursion program :” +num);

if(num==1) return 1;

recursion(–num);

}

 

recursion(20); // calling for 20

 

Q 50: Write a program to demonstrate self-executing function.

Ans – Program is as follow.

(function(){

console.log(‘I am a self executing fucntion!’);

})();

 

 

Q 51:  Which function is used to print the output?

  1. a) system.out.println
  2. b) log
  3. c) console.log
  4. d) print

Ans : c

 

Q 52: Which of following is not a mouse event?

  1. a) mousescroll
  2. b) dbclick
  3. c) mouseover
  4. d) mouseleave

Ans : a

 

Q 53:  How to go back with javaScript?

  1. a) history method
  2. b) back method
  3. c) go method
  4. d) p

Ans : c

 

Q 54:  What is not a valid value of a boolean?

  1. a) true
  2. b) false
  3. c) null
  4. d) NaN

Ans : d

 

Q 55:  What is the output for (‘0’===0 || “1”==1)?

  1. a) true
  2. b) false
  3. c) undefined
  4. d) NaN

Ans : a

 

Q 56:  How to break the for loop in javaScript?

  1. a) with continue statement
  2. b) with break statement
  3. c) create exception in the flow
  4. d) stop the loop manually with stop button of complier

Ans : b

 

Q 57:  Is Recursion possible in JS?

  1. a) Not at all possible
  2. b) Only possible with JS version of 2.0 and higher
  3. c) Always possible in all versions
  4. d) It is only possible if the operation is related to numbers and not strings

Ans : c

 

Q 58:  What is below example referring to?

function valueWrapper(num)

{

let internalValue = num;

return () => internalValue;

}

 

  1. a) This is an example of callable method
  2. b) This is an example of for loop in JS 3.0
  3. c) This is an example of angular functions
  4. d) This is an example of closure methods

Ans : d

 

Q 58:  How to get length of a String(str) in JS?

  1. a) str.length
  2. b) str.length()
  3. c) str.size()
  4. d) length(str)

Ans : a

 

 

Q 59: Which tag is used to write JS in HTML?

  1. a) <script>
  2. b) <js>
  3. c) <anyScript>
  4. d) <xml>

Ans : a

 

 

Q 60:  Is it mandatory to have a <script> tag in each JS file?

  1. a) No
  2. b) Yes
  3. c) Sometimes whenever there is a calculation
  4. d) Only required if JS is making REST service call

Ans : a

 

Q 61:  Classes can also be defined in JS?

  1. a) No
  2. b) Yes
  3. c) Only applicable if JS version is 2.7
  4. d) Classes can only be defined in Java

Ans : b

 

 

Q 62:  Is it mandatory to have a <script> tag in each JS file?

  1. a) No
  2. b) Yes
  3. c) Sometimes whenever there is a calculation
  4. d) Only required if JS is making REST service call

Ans : a

 

Q 62:  Which one is not reserved keyword?

  1. a) string
  2. b) script
  3. c) let
  4. d) ver

Ans : d

 

Q 63:  How many fields on a page is focused at once?

  1. a) three if there is a 3 D image
  2. b) only one
  3. c) none
  4. d) Depends on code, it can be N numbers

Ans : b

 

Q 64:  What will be the output for below code?

 

function testFunction() {

var a = 34;

return function(b) {

console.log(a-b);

}

}

var functionObj = testFunction();

functionObj(5);

 

  1. a) NaN
  2. b) Error
  3. c) 29
  4. d) -5

Ans : c

 

Q 65:  Which of following is true about javascript features?

  1. a) lightweight
  2. b) Interpreted
  3. c) Independent
  4. d) all

Ans : d

 

Q 66:  Which of following are not a valid browser?

  1. a) chrome
  2. b) sefari
  3. c) Firefox
  4. d) Internet Explorer

Ans : b

 

Q 67:  Which of the following is true about routing?

  1. a) Routing can be implemented with Javascript
  2. b) This is a default feature of browser
  3. c) It can only be implemented with frameworks like angular or backbone
  4. d) Routing is handled by server

Ans : a

 

Q 68:  Does JS support events?

  1. a) Yes, it supports mouse and keyboard events
  2. b) No
  3. c) It supports mouse and keyboard event if they provided by only the logged in user
  4. d) Only JS version 2.8 and above supports it

Ans : a

 

Q 69:  What is the accurate syntax for replace method for String (str)?

  1. a) relace(str, “old”, “new”)
  2. b) str.replace(“old”, “new”);
  3. c) replace(old, new)
  4. d) a and c

Ans : b

 

Q 70:  Which symbol is used in arrow functions?

  1. a) <=
  2. b) <-
  3. c) ->
  4. d) =>

Ans : d

 

 

Q 71:  is ‘false’==false?

  1. a) No it is not equal because one is a string and another one is a Boolean
  2. b) Yes
  3. c) It will be equal if == is replaced with ===
  4. d) It is not equal but throws error in the code.

Ans : a

 

Q 72:  What keyword should be used to make sure code is not failing silently?

  1. a) UnMute
  2. b) UnSilent
  3. c) use strict
  4. d) throw error

Ans : c

 

Q 73:  is parseFloat() a valid function of JS?

  1. a) No it is a java function
  2. b) Yes
  3. c) No it is not a JS function but It can be used in JS with some jar files
  4. d) Yes only for version 2.8 and above

Ans : b

 

Q 74:  What will be the output for below program?

(function(){

var a,b = 20;

})();

 

if(typeof a !== ‘undefined’)

console.log(“1”);

else if (typeof b !== ‘undefined’)

console.log(“2”);

else

console.log(“3”);

 

  1. a) 1
  2. b) 2
  3. c) 3
  4. d) error at declaration

Ans : c

 

Q 75:  What will be the output for below program?

(function(){

console.log(100+true)}

)();

 

  1. a) 101
  2. b) 100true
  3. c) error
  4. d) 100

Ans : a

 

Q 76:  What will be the output for below program?

(function(){

console.log(!”testing”)}

)();

 

  1. a) !testing
  2. b) testing
  3. c) invalid
  4. d) false

Ans : d

 

Q 77:  What will be the output for below program?

(function(){

try{

var i=1/0;

}catch(e){

console.log(“1”);

}

console.log(“2”);

}

)();

 

  1. a) 2
  2. b) 1
  3. c) 12
  4. d) throws exception and no output

Ans : a

 

Q 78:  What will be the output for below program?

function currying(base){

return function(num){

return base * num;

}

}

 

var multiplySeven = currying(7);

console.log(multiplySeven(12));

 

  1. a) 12*7
  2. b) 84
  3. c) 12
  4. d) throws exception and no output

Ans : b

 

Q 79:  What will be the output for below program?

var str = “100 questions”;

var num = str.search(/questions/i);

console.log(num);

 

  1. a) Invalid regular expression
  2. b) search is not a valid function for JS
  3. c) -1
  4. d) 4

Ans : d

 

Q 80:  What will be the output for below program?

console.log(location);

 

  1. a) location
  2. b) false
  3. c) prints location object which has href etc.
  4. d) location is undefined

Ans : c

 

 

Q 81:  What will be the output for below program?

console.log(typeof(NaN));

 

  1. a) error – typeof only application for data types
  2. b) number
  3. c) String
  4. d) false

Ans : b

 

Q 82:  What will be the output for below program?

console.log(-5%2==0);

 

  1. a) false
  2. b) true
  3. c) error
  4. d) -1

Ans : a

 

Q 83:  Which method converts a string to upper case?

  1. a) Javascript doesn’t support it
  2. b) toUppercase
  3. c) toupperCase
  4. d) toUpperCase()

Ans : d

 

Q 84:  is it possible to make drawings with JS?

  1. a) Yes
  2. b) No
  3. c) Yes, with the help of HTML it is achievable
  4. d) Yes, only with JS 2.8

Ans : a

 

Q 85:  What is hoisting?

  1. a) It is a kind of web hosting
  2. b) It is a JS feature to move the declaration on top
  3. c) It is a property of string
  4. d) Not related to JS

Ans : b

 

Q 86:  Which variables JS supports?

  1. a) Local
  2. b) Global
  3. c) Local and Global both
  4. d) Temp

Ans : c

 

Q 87:  What does const keyword do?

  1. a) Const is not a valid keyword
  2. b) Value of const will not change
  3. c) It is same as var
  4. d) Not related to JS

Ans : b

 

Q 88:  Which of following is not a debugging technique?

  1. a) debugger
  2. b) alert
  3. c) log
  4. d) break

Ans : d

 

Q 89:  Does JS supports automatic type conversion?

  1. a) Yes
  2. b) No
  3. c) Only version 2.9 and above
  4. d) It depends on the browser used

Ans : a

 

 

Q 90:  Are var and let same?

  1. a) Yes
  2. b) Both are not comparable
  3. c) No, there is a slight difference with the scope
  4. d) let is faster than var

Ans : c

 

Q 91:  Which of the following method of Boolean object returns a string depending upon the value of the object?

  1. a) toUpperCase()
  2. b) valueOf()
  3. c) None
  4. d) toString()

Ans : d

 

Q 92:  Select a function of Array object which returns a new array comprised of the current array /or its value(s)?

  1. a) pop()
  2. b) push()
  3. c) pull()
  4. d) queue()

Ans : a

 

Q 93:  Why do we need JS frameworks even we have JS itself?

  1. a) Frameworks are not required
  2. b) They are built on top of JS and makes our life easy
  3. c) Both are separate things
  4. d) It depends on the requirement

Ans : b

 

Q 94:  What is the latest version of JS?

  1. a) ECMAScript 2020
  2. b) ECMAScript 2014
  3. c) ECMAScript 2011
  4. d) ECMAScript 2018

Ans : d

 

Q 95:  Which parameter passing approach is application in JS?

  1. a) Pass by value
  2. b) Pass by reference
  3. c) a and b
  4. d) None

Ans : c

 

Q 96:  What is ‘this’?

  1. a) This is basically pointing to the current object
  2. b) Not supported in JS
  3. c) ‘this’ is a method which interns call object creation
  4. d) it is a regular expression property

Ans : a

 

Q 97:  Can we implement Inheritance in JS?

  1. a) Yes
  2. b) No, it is for Java only
  3. c) Yes if it is a mobile website
  4. d) No, for chrome browsers

Ans : a

 

Q 98:  What will be the output for below program?

console.log(0.4 + 0.2);

 

  1. a) 0.40.2
  2. b) 0.42
  3. c) 0.6000000000000001
  4. d) 0.6

Ans : c

 

Q 99:  What will be the output for below program?

const power = (base, exponent) =>

{ let result = 1;

for (let count = 0; count < exponent; count++)

{ result *= base; } return result; };

 

console.log(power(2,6));

 

  1. a) Error – const is not a valid keyword
  2. b) 64
  3. c) wrong implementation for arrow method
  4. d) 12

Ans : b

 

Q 100:  How many times text “printed” will be printed?

function looping(num){

 

for(var i=0; i<num; i++){

if(i>20) continue;

console.log(“printed”);

if(i<20) break;

}

}

looping(100);

 

  1. a) 1
  2. b) 0
  3. c) 100
  4. d) 20

Ans : c

 

Javascript Interview Questions And Answers PDF

You can also download JavaScript interview questions and answers pdf from the link below :

Javascript Interview Questions

Hope the above will help you in preparing for JavaScript interviews.