70+ JavaScript Interview Questions To Hire Or Get Hired With

4802 words Read time 23:56

JavaScript has seen a recent resurgence in the software development lifecycle. And that's primarily because of Facebook's more widely adopted methodologies for being able to scale JavaScript. Also because of the users more widely adopted needs to have their content or software via web applications served to them more quickly, almost in real-time. Below are some great examples of JavaScript interview questions that might be asked of you in the interview process, most likely by phone or through a pair programming session. The answers below are examples, of course. And while they are correct, you should always take the time to put together your own interview question answers. Making them your own will not only provide you more confidence in the answer but it will also allude to your hiring manager that you know the subject matter well. If you are using these questions for hiring, I strongly suggest dividing up these questions between the multiple colleagues who may be performing the interviews.

1. What is JavaScript?

JavaScript is an interpreted programming language that is lightweight in nature and offers object-oriented capabilities that allow users to build interactivity into otherwise static HTML pages.

2. Name the main features of JavaScript

• It is a lightweight and interrogated programming language
• It is designed to create network-centric applications
• It is complementary and can be integrated with Java.
• It is complementary and can be integrated with HTML.
• It is an open cross-platform

3. What are the main advantages of using JavaScript?

• Less server interaction: It is possible for the user to validate the input before the page is sent off to the server.
• Allows for immediate feedback: Users do not necessarily have to wait for the page to reload in order to confirm whether they have forgotten to enter something
• Increased interactivity: While using JavaScript, it is possible for you to create an interface that reacts when a user hovers over it with a mouse or activates the platform via the keyboard.

4. Why is JavaScript referred to as the Richer Interface?

JavaScript has the ability to include items such as sliders and drag-and-drop components that give provide a rich interface to your site’s visitors.

5. Is JavaScript a case-sensitive programming language?

JavaScript is indeed a case-sensitive programming language which means that keyword function names, variables or any other identifiers must be entered with a consistent capitalization of letters.

6. Name the valid scopes of variables in JavaScript?

• Global variables: These are variables that have a global scope-meaning that they are visible everywhere within your JavaScript code.
• Local variables: These are variables that are only visible within the function where they are defined. This further means that function parameters are local to that specific function.

7. What are the set standards for naming variables in JavaScript?

When naming your variables in JavaScript, keep the following in mind:

• Any JavaScript reserved keyword should not be used as a variable name. For example, break and Boolean variable names are invalid.
• JavaScript names should never begin with a numerical figure (0-9). They must always begin with a letter or an underscore character. For example, 123test is not a valid variable name but _123test is.
• The names of variables in JavaScript are case-sensitive. For example, name and Name are two different variable names.

8. How do you read a cookie using JavaScript?

Reading a cookie on JavaScript is almost as easy as creating one by the simple fact that the document. Cookie object is the cookie. The document.cookie string will keep a list of name = value pairs separated by semicolons, where name is the name of a cookie and value is its string value. The user can then use strings’ split () function to break the string into values and keys.

9. How do you delete a Cookie when using JavaScript?

Sometimes, you want to delete a Cookie so that any subsequent attempts to read the Cookie return nothing. For this to happen, you only need to set the date of expiration to a time in the past.

10. When using JavaScript, what is the Date object?

This is a data-type built into the JavaScript language and is created with a new Date ( ). Once a Date object has been created in JavaScript, there are a number of methods that allow you to operate on it. Most of these methods will allow you to set the millisecond, second, minute, hour, day, week, month and year fields using either local time or universal time.

11. How is a webpage printed when using JavaScript?

This functionality is implemented by using the print function of a window object. When executed, the print () function will print the current webpage.

12. How are exceptions handled in JavaScript?

Exception capabilities were introduced in the most recent version of JavaScript. JavaScript implements, try, catch and construct operators to handle exceptions.

13. What is the role of the onError event handler in JavaScript?

The oneError event handler is designed to facilitate error handling in JavaScript. Whenever an exception occurs on a page, the error even it fired on the window object. There are three main pieces of information that the oneError event handle provides in order to identify the actual nature of the error.

• Error message: This is the same message that the browser would display in the event of an error.
• URL: This is the file where the error occurred.
• Line number: This is the exact line number that caused the error in a given URL.

14. What are some of the disadvantages of using JavaScript?

• The client-side of JavaScript does not allow any form of reading or writing files for security reasons.
• JavaScript cannot be used for networking applications because no such support is available.
• JavaScript lacks any multiprocessing or multithreading abilities.

15. What is the main difference between Java and JavaScript?

Java is a complete programming language while JavaScript is a coded program that is introduced to HTML pages. Both languages are not dependent upon each other since they are designed for different purposes. Whereas Java is a structured programming language similar to C++ or C#, JavaScript is a client-side scripting language.

16. What are the main data types in JavaScript?

• Number
• String
• Boolean
• Object
• Undefined

17. What is the function of the is NaN function?

Returns true when the argument is not a numerical number otherwise, it is considered to be false.

18. Which is faster between JavaScript and ASP script?

JavaScript is faster compared to ASP since it does not require the assistance of the web server to execute. ASP, on the other hand, is slower as it is a server-side language.

19. List the data types that are supported by JavaScript

• Undefined
• Null
• Boolean
• String
• Symbol
• Number
• Object

20. What is referred to as negative infinity in JavaScript?

his is the number arrived at after dividing a negative number by zero.

21. Can a JavaScript code be broken into several lines?

It is possible to break a JavaScript code into several lines using a backslash, '\', at the end of the first line within a string statement.


document.write ("This is \a program");
If you try to change to a new line statement while not being within a string statement, then the break is ignored by JavaScript. The code below is perfectly okay though not recommended as it affects debugging.
var x=1, y=2,

22. What is the name of the company that developed JavaScript?

avaScript was developed by the software company Netscape

23. What is meant by undeclared and undefined variables?

• Undeclared variables are those variables that do not exist in a program in addition to not being declared. If a program tries to read an undeclared variable, a runtime error occurs.
• Undefined variables, on the other hand, are those that have been declared in a program but have not been allocated a value. When a program tries to read an unidentified variable, an undefined value is returned.

24. How do timers work in JavaScript? Elucidate on the downside of using timers

Timers are used in the execution of a set piece of code and to also repeat the code at a given time interval. This process is done by functions such as; setTimeout, setInterval and clearInterval.

The setTimeout function starts a timer that sets in motion a particular function after the mentioned delay. The setInterval function repeatedly executes the said function and only halts the process if a cancellation is done. The clearInterval (id) function then instructs the timer to stop.

The drawback of using timers is that they are operated within a single thread which them means that they sometimes queue up while waiting to be executed.

25. Name the looping structures in JavaScript

• For
• While
• do-while loops

26. What are the main ways of reading and writing a file using JavaScript?

• By using JavaScript extensions
• By using web pages and Active X objects

27. What is meant by NULL in JavaScript?

The NULL value in JavaScript represents no value or object. It, therefore, implies no object, null string, valid Boolean value, number or array object.

28. Name the three types of Pop up boxes in JavaScript

• Alert
• Confirm and
• Prompt

29. What is Void (0) used for in JavaScript?

• To prevent the page from refreshing
• To call a different method without refreshing the page

30. How can a user force a page to load a different page on JavaScript?

The following code can be inserted to force a page to load another page.

script language="JavaScript" type="text/javascript"
!-- location.href="http://newhost/newpath/newfile.html"; //-- /script

31. In JavaScript, what are some of the disadvantages of using inner HTML?

Some of the disadvantages of using HTML are that;
• Replacement of content takes place everywhere
• "Appending to innerHTML" cannot be used.
• Even when +=like "innerHTML = innerHTML + 'html'" is used, the old content is still replaced by HTML.
• Content in innerHTML is much slower since it is re-parsed and built into elements.
• innerHTML does not allow for any validation of data meaning that it is potentially possible to insert a valid and broken HTML in the document and break it.

32. What is the difference between a break statement and a continue statement.

A break statement exists in a current loop whereas a continue statement continues with the next statement in a loop.

33. Name the two main groups of data-types in JavaScript.

• Primitive-these are number and Boolean data types
• Reference types-these are complex data types such as strings and dates

34. Name the different types of errors in JavaScript

There are three main errors in JavaScript namely;
• Load time errors-These are errors that arise when loadi8ng a web page.
• Runtime errors-These are errors that occur as a result of misuse on the command inside the HTML language.
• Logical errors-Logical errors arise after a bad logic is performed on a function that is having a different operation.

35. In JavaScript, what is referred to as Push method?

The Push method in JavaScript adds or appends one or more elements to the end of an Array. Using the Push method, it is possible to append multiple elements by passing multiple arguments.

36. What is strict mode in JavaScript and how is it enabled?

The role of Strict Mode is to add certain compulsions to JavaScript. During the process, JavaScript displays errors in a piece of codes meaning that the said codes are problematic and potentially unsafe-these codes probably never exhibited errors before. Strict mode is also responsible for slowing problems that hamper effective functioning of JavaScript engines.

Strict Mode is enabled when the string lateral "use strict" is added above the file as illustrated in the example below.

function myfunction() {
"use strict";
var v = "This is a strict mode function";

37. How are JavaScript codes hidden from an old browser that does not support JavaScript

When you want to hide JavaScript codes from old browsers;
• Add "" without the quotes in the code just before the script tag.

38. What is ECMAScript in JavaScript?

ECMAScript stands for European Computer Manufacturers Association Script that is the specification for the standard scripting language. This script has a standardized JavaScript which is the reason why JavaScript is the best scripting language for the implementation of ECMAScript.

39. In JavaScript, what is the difference between Attributes and Properties?

Attributes provide more details regarding an element such as id, type, value etc. Property on the other hand is the assigned value to property such as type="text", value='Name' etc.

40. Name the different ways that an HTML element can be accessed in a JavaScript code

Below are some the ways that an HTML element can be accessed in a JavaScript code:

• getElementById ('idname'): identifies an element by its ID name
• getElementsByClass('classname'): identifies all the elements that have the given classname.
• getElementsByTagName('tagname'): identifies all the elements that have the given tag name.
• querySelector():takes CSS style selector (like #id/.classname/tagname) and returns the first selected element.
• querySelectorAll(): Similar to querySelector, returns a NodeList of HTML elements.

41. How many ways can a JavaScript code be involved in a HTML file?

• Inline
• Internal
• External

42. What are some of the recent ways of defining a variable in JavaScript?

In JavaScript, a variable can be defined in the following ways;

• (var)
• (const)
• (let)

Both (const) and (let) are the latest ways of defining a variable in JavaScript that were introduced in the ES-2015(ES6 version).

43. What is Typed Language and how many types of Typed Language are there in JavaScript?

• In Typed Languages, values are associated with other values and not with variables. There are two types of Typed Language in JavaScript namely:
• Dynamically: here, the variable can hold have multiple types; for example in JS, the variable can take number, chars.
• Statically: here, the variable can only hold a single type. For example in Java, a variable can only take a set of characters and nothing more.

44. Name ways Type Conversion is possible in JavaScript?

(i) Number to String Conversion.

• toString: Other data type to String.
• val = (5).toString(); converts integer to string.
• val = (true).toString(); converts boolean to string.
• Convert from Number/Boolean/Date to String.
• Number to String: String(9) converts num to string
• Boolean to String: String(true) converts bool to str
• Date to String: String(new Date()) date to string
• Array to String: String ([2, 2, 2]) Array to string.

(ii) String to Number Conversion.

• parseInt: String to Int only (no decimals)
• val = parseInt('11'); outputs 100 as number
• val = parseFloat('22.22') outputs 22.22 as float
• Convert from String/Boolean to Number/Date.
• String to Number: Number('9') converts str - num
• (9).toFixed(4) gives 9.0000 as the output
• Boolean to Number: Number (true) converts to no.
• Null to Number: Number (null) converts to no. (0)
• Chars to Number: Number ('ss') give NaN.

45. What is namespacing in JavaScript?

Namespacing is used to group the desired functions or variables under a unique name. This grouping is meant to improve modularity in the coding as well as enabling the reuse of codes.

46. What is the difference between Session storage and Local Storage?

Local storage is retained unless manually cleared through programs or settings while Session Storage leaves when the browser is closed.

47. What are the most commonly used JavaScript frameworks today?

The most commonly used JavaScript frameworks are:

• Angular
• React
• Vue

48. What are the out of the Mouse Events in JavaScript?

• Click: A single click
• dblclick: A double click
• mousedown: When the mouse button is clicked down.
• mouseup: When the mouse button is released up.
• mouseenter: When the mouse cursor enters an external element.
• mouseleave: When the mouse cursor leaves an external element.
• mouseover: When the mouse cursor enters an internal and external element.
• mouseout: When the mouse cursor leaves an internal and external element.
• mousemove: When the mouse cursor is moved.

49. What are Self-Executing Functions in JavaScript?

These are the functions that are executed immediately after their definition. They are also referred to as Immediately Invoked Function Expressions (IIFE’s) with the syntax

(function () {
console.log ('in iffe');

50. What is the role of anonymous functions in JavaScript?

Anonymous functions allow for the creation of a function that has no name by the developer. This means that anonymous functions can store a little bit of functionality in a variable and still be able to pass that piece of functionality around.

51. What is the main difference between Named Functions and Anonymous Functions?

Named functions to exist even when not called while anonymous functions only exist when called.

52. Explain the difference between synchronous and asynchronous codes in JavaScript

Asynchronous code in JavaScript refers to the codes that need to be executed before anything else happens while asynchronous codes are not dependent on other actions for them to be executed.

53. What two functions are used to create an HTML element dynamically?

In order to dynamically create an HTML code, the following functions are used;

var ele = createElement ('elementName'), which creates an element and parentElement.appendChild(ele)

54. What are cookies in JavaScript?

Cookies are variables that are stored on a client/visitors’ machine. It is through the Cookies that a server is able to identify the client and the client’s transactions.

55. Compare JavaScript vs AngularJS

JavaScript AngularJS Programming language Framework used to create mobile and web applications It supports loops, conditions and exceptions Does not support loops, conditions, and exceptions Does not support filters Supports filters Helps in building frameworks Built by using JavaScript

56. Name some of the common problems associated with using global variables.

Despite the fact that global variable is easy to use, they have their own shortfalls when it comes to functionality. One of the most common problems is that clashing of variable names between different local and global scopes often occur. Another common problem is that the code used for global variables is also difficult to test and debug.

57. What are some of the disadvantages of using innerHTML in JavaScript?

• Using innerHTML is JavaScript is much slower compared to other variables because its content takes time to get re-parsed.
• When using innerHTML, the contents is replaced in JavaScript
• Appending to InnerHTML cannot be used properly.
• Using innerHTML breaks the JavaScript document as no validation is required by this variable. This means that any type of valid innerHTML including broken HTML can be used which is sometimes problematic.
• When using innerHTML, old content is easily replaced.

58. What are the two main data-types used in JavaScript?

There are two main types of data in JavaScript namely;

• Reference type-these are complex data-types that include dates and strings
• Primitive type-These are data types that include data

59. Name the two main types of functions that are supported by JavaScript

There are two main functions that are supported by JavaScript namely;
• Anonymous function-this function lacks a name which is the main differentiating factor between it and the named function.
• Named function: This function has a proper and specific name.

60. Define the meaning of the term “callback” when using JavaScript

This is a function of JavaScript that is passed as an option or argument of JavaScript. In simple terms, callbacks are defined as simple events where callers are given calls in order to react to different triggered situations.

61. How do you create a class when using JavaScript?

There is no standard definition of class in JavaScript. To mimic the action of class in JavaScript, the example below can be used to declare a class. For example, creating a class in JavaScript that takes two parameters name and roll as property. The code generated will appear as the one below.

Function Student (name .roll) { = name;
this.roll = roll;

62. What is the main difference between undefined variables and undeclared variables?

• Undefined variables are those variables that are declared in the program. However, the values of undefined variables are not provided in JavaScript. If the user attempts to read the undefined value, the value is returned.
• Undeclared variables, on the other hand, are those that do not even exist on a program in addition to not being declared properly. If one tries to read the value, a runtime error occurs.

63. What types of functional components are available in JavaScript?

There are two main types of functional components in JavaScript programming language namely;

• Nested functions: Just as their names suggest, nested functions are those functions that are included in other functions. Nested functions are called each time the invoked functions are provided.
• First class functions: These are functions used for first-class objects. In most cases, first-class functions are used as arguments and opinions against other functions. In first-class functions, data structures stores the values of the functional components and the variables too can be stored properly in the data structures.

64. How are private members declared when using JavaScript?

When using JavaScript, private members are declared using var keyword as well as the construction functions.

Function Student (name, roll){
var id= ABCD123; = name;
this.roll = roll;

65. What is prototype property when using JavaScript?

A prototype property is used to add new members to an existing object. Ideally, every JavaScript object has a prototype property internally.

Function Student (name, roll){ = name;
this.roll = roll;
var student1 = new Student(’sangeeta’,30);
Student.prototype.mark = 100;

66. What are the properties of Screen Objects?

These are objects in JavaScript that are used to read different types of information from a client’s screen. The main properties of screen objects include;

• Provides the height of the client’s screen • Provides images appearing on the client’s screen
• Provides the width of the screen
• Since the client’s screen often includes the taskbar, screen objects give the total height.
• The width of the client’s screen also includes the taskbar and hence the screen objects provide the total width available to the users.

67. What is a constructor property when using JavaScript?

This is an object that maintains a reference to its creator function.

The example below shows how a user can create a student object and calling the constructor property on it.
function Student( name, mark){
this. name = name; this. mark =mark;
var student 1 = new Student (’sandeep’, 123);
console.log (student1.constructor);
function Student(name, mark){ = name;
this.mark = mark;
var student1 = new Student(’sandeep’ ,123);
console.log (student1.constructor);

68. How can inheritance be achieved in JavaScript using Prototype property.

Inheritance in JavaScript can be achieved by following the steps below;

Child class prototype should point to parent class object.
.prototype = new ();

Reset the child class prototype constructor to point self. .prototype.
Constructor = student;
The example below shows ScienceStudent class as a child class of Student class.
Function Student (name){;
Student.prototype.savIyType = function(){
consoe.log (”I am student type”)
function Science Student (name){
Science Student.prototvpe = new Student();
ScienceStudent.prototvpe.constructor = Science Student;
var student2 = new Science Student(sangeeta’);
console.log(student2 .sayMyType ());

69. What is JavaScript Hoisting?

This is a method used when an interpreter runs the code and all the variables are hoisted to the top of the original scope. This means that if there is a variable that has already been declared anywhere inside the JavaScript code, it is brought to the top. JavaScript Hoisting is only applicable during the declaration of a variable and not for the initialization of a variable. Also, functions are hoisted to the top while the function explanations are not hoisted to the top.

70. What are some of the major characteristics of Strict Mode in JavaScript?

• Stops developers from creating global variables
• Stops developers from using duplicate parameters
• Restricts the user from using JavaScript keyword as a variable name
• Declared using the “use strict” keyword at the beginning of the script
• Strict mode is supported by all browsers

71. What is the syntax of a Self-Invoking function?

(function () {
return () } () ;

Sample: Software Testing Help

Example for Self-Invoking

2. Name the distinct types of Error Name Property.

• Range Error: This error occurs when a number outside the range is used.
• Syntax Error: This error occurs when we use the incorrect syntax.
• Reference Error: This error occurs if used an undeclared variable
• Eval Error: This error arises due to the error in eval (). New JavaScript version doesn’t have this error
• Type Error: Occurs, when the value used, is outside the range of types used • URI Error: This error arises due to the usage of illegal characters.

73. What are some of the advantages of using External JavaScript?

• Separation of Code is done.
• Code Maintainability is Easy.
• Performance is better.

74. What is the difference between test () and exec () methods?

Both of these tests are RegExp expression methods.
• When you use test (), a string of a given pattern is searched and if there is a matching text present in the string, the test returns a Boolean value ‘true’ and else it returns ‘false’.
• In exec (), a string of a given pattern is searched and if there is a matching text found in the pattern, then the pattern itself is returned and else returns a null value.

Share this article


Reach out