JavaScript Interview Questions

1) What is JavaScript and how does it work? 

A scripting language is JavaScript. It is not the same as the Java programming language. It is a lightweight, object-based, cross-platform translation language. It’s a popular method for client-side validation. The JavaScript Translator (which is built within the browser) is in charge of translating JavaScript code for web browsers. More information is available. 

2) Describe some of JavaScript’s features. 

The following are some of JavaScript’s features: 

  • A programming language that can be easily interpreted. 
  • It’s ideal for network-centric applications. 
  • In addition to Java, 
  • Open source, cross-platform complement to HTML

3) Who created JavaScript, and what was the original name of the language? 

Brendan Eich, a former Netscape programmer, created JavaScript. In September 1995, Brendan Eich created this new programming language in under ten days. JavaScript was formerly known as Mocha when it was first released. After that, it was known as Live Script, and then JavaScript. 

4) Describe some of the benefits of JavaScript. 

The following are some of the benefits of JavaScript: 

  • There is less contact with the server. 
  • The visitors receive rapid feedback. 
  • The level of interactivity is high, and the interfaces are more diverse.

5) Describe some of JavaScript’s drawbacks. 

The following are some of JavaScript’s drawbacks: 

  • Multithreading isn’t supported. 
  • Multiprocessing isn’t supported. 
  • It is not permitted to read or write files. 
  • There is no networking application support. 

6) In JavaScript, create a named function. 

A named function is a function that has a name at the time of definition. As an example, 

function msg()  


  document.writeln(“Named Function”);  



7) Name the types of functions

The types of function are:

Named – These type of functions contains name at the time of definition. For Example:

function display()  


  document.writeln(“Named Function”);  



Anonymous – These type of functions doesn’t contain any name. They are declared dynamically at runtime.

var display=function()  


  document.writeln(“Anonymous Function”);  



8) Define anonymous function

It is a function that has no name. These functions are declared dynamically at runtime using the function operator instead of the function declaration. The function operator is more flexible than a function declaration. It can be easily used in the place of an expression. For example:

var display=function()  


  alert(“Anonymous Function is invoked”);  



9) Can an anonymous function be assigned to a variable?

Yes, you can assign an anonymous function to a variable.

10) In JavaScript what is an argument object?

The variables of JavaScript represent the arguments that are passed to a function.

11) Define closure.

In JavaScript, we need closures when a variable which is defined outside the scope in reference is accessed from some inner scope.

var num = 10;  

function sum()   





12) If we want to return the character from a specific index which method is used?

The JavaScript string charAt() method is used to find out a char value present at the specified index. The index number starts from 0 and goes to n-1, where n is the length of the string. The index value can’t be a negative, greater than or equal to the length of the string. For example:

var str=”Javatpoint”;    


13) What is the difference between JavaScript and JScript?

Netscape provided the JavaScript language. Microsoft changed the name and called it JScript to avoid the trademark issue. In other words, you can say JScript is the same as JavaScript, but Microsoft provides it.

14) How to write a hello world example of JavaScript?

A simple example of JavaScript hello world is given below. You need to place it inside the body tag of HTML.

15) What are the key differences between Java and JavaScript? / How is JavaScript different from Java?

A list of key differences between Java and JavaScript

Java is a complete and strongly typed programming language used for backend coding. In Java, variables must be declared first to use in the program, and the type of a variable is checked at compile-time.JavaScript is a weakly typed, lightweight programming language (most commonly known as scripting language) and has more relaxed syntax and rules.
Java is an object-oriented programming (OOPS) language or structured programming languages such as C, C++, or .Net.JavaScript is a client-side scripting language, and it doesn’t fully support the OOPS concept. It resides inside the HTML documents and is used to make web pages interactive (not achievable with simple HTML).
Java creates applications that can run in any virtual machine (JVM) or browser.JavaScript code can run only in the browser, but it can now run on the server via Node.js.
The Java code needs to be compiled.The JavaScript code doesn’t require to be complied.
Java Objects are class-based. You can’t make any program in Java without creating a class.JavaScript Objects are prototype-based.

16) How to use external JavaScript file?

I am assuming that js file name is message.js, place the following script tag inside the head tag.

17) Is JavaScript case sensitive language?

Yes, JavaScript is a case sensitive language. For example:

Var msg = “JavaScript is a case-sensitive language”; //Here, var should be used to declare a variable  

function display()   


document.writeln(msg); // It will not display the result.  



18) What is BOM?

BOM stands for Browser Object Model. It provides interaction with the browser. The default object of a browser is a window. So, you can call all the functions of the window by specifying the window or directly. The window object provides various properties like document, history, screen, navigator, location, innerHeight, innerWidth,

19) What is DOM? What is the use of document object?

DOM stands for Document Object Model. A document object represents the HTML document. It can be used to access and change the content of HTML.

20) What does the window object do? 

The window object, which represents a browser window, is created automatically by the browser. It is not a JavaScript object. It’s a browser element. 

The popup dialogue box is displayed using the window object. Let’s have a look at the description.

The window object is used to display the popup dialog box. Let’s see with description.

alert()displays the alert box containing the message with ok button.
confirm()displays the confirm dialog box containing the message with ok and cancel button.
prompt()displays a dialog box to get input from the user.
open()opens the new window.
close()closes the current window.
setTimeout()performs the action after specified time like calling function, evaluating expressions.

21) What is the use of history object?

The history object of a browser can be used to switch to history pages such as back and forward from the current page or another page. There are three methods of history object.

  • history.back() – It loads the previous page.
  • history.forward() – It loads the next page.
  • history.go(number) – The number may be positive for forward, negative for backward. It loads the given page number.

22) How to write a comment in JavaScript?

There are two types of comments in JavaScript.

  • Single Line Comment: It is represented by // (double forward slash)
  • Multi-Line Comment: Slash represents it with asterisk symbol as /* write comment here */

23) How to create a function in JavaScript?

To create a function in JavaScript, follow the following syntax.

function function_name(){  

//function body  


24) What is the use of a WeakMap object in JavaScript?

The JavaScript WeakMap object is a type of collection which is almost similar to Map. It stores each element as a key-value pair where keys are weakly referenced. Here, the keys are objects and the values are arbitrary values.

25) What is the difference between == and ===?

The == operator checks equality only whereas === checks equality, and data type, i.e., a value must be of the same type.

26) How to write HTML code dynamically using JavaScript?

The innerHTML property is used to write the HTML code using JavaScript dynamically. Let’s see a simple example:

document.getElementById(‘mylocation’).innerHTML=”<h2>This is heading using JavaScript</h2>”;  

27) How to write normal text code using JavaScript dynamically?

The innerText property is used to write the simple text using JavaScript dynamically. Let’s see a simple example:

document.getElementById(‘mylocation’).innerText=”This is text using JavaScript”;   

28) How to create objects in JavaScript?

There are 3 ways to create an object in JavaScript.

  • By object literal
  • By creating an instance of Object
  • By Object Constructor

Let’s see a simple code to create an object using object literal.

emp={id:102,name:”Rahul Kumar”,salary:50000}   

29) How to create an array in JavaScript?

There are 3 ways to create an array in JavaScript.

  • By array literal
  • By creating an instance of Array
  • By using an Array constructor

Let’s see a simple code to create an array using object literal.

var emp=[“Shyam”,”Vimal”,”Ratan”];   

30) What is the use of a Set object in JavaScript?

The JavaScript Set object is used to store the elements with unique values. The values can be of any type i.e. whether primitive values or object references. 

31) What is the use of a WeakSet object in JavaScript?

The JavaScript WeakSet object is the type of collection that allows us to store weakly held objects. Unlike Set, the WeakSet are the collections of objects only. It doesn’t contain the arbitrary values.

32) What is the use of a Map object in JavaScript?

The JavaScript Map object is used to map keys to values. It stores each element as key-value pair. It operates the elements such as search, update and delete on the basis of specified key.

33) Difference between Client side JavaScript and Server side JavaScript?

Client-side JavaScript comprises the basic language and predefined objects which are relevant to running JavaScript in a browser. The client-side JavaScript is embedded directly by in the HTML pages. The browser interprets this script at runtime.

Server-side JavaScript also resembles client-side JavaScript. It has a relevant JavaScript which is to run in a server. The server-side JavaScript are deployed only after compilation.

34) In which location cookies are stored on the hard disk?

The storage of cookies on the hard disk depends on the OS and the browser.

The Netscape Navigator on Windows uses a cookies.txt file that contains all the cookies. The path is c:\Program Files\Netscape\Users\username\cookies.txt

The Internet Explorer stores the cookies on a file username@website.txt. The path is: c:\Windows\Cookies\username@Website.txt.

35) What’s the difference between event.preventDefault() and event.stopPropagation() methods in JavaScript?

In JavaScript, the event.preventDefault() method is used to prevent the default behavior of an element.

For example: If you use it in a form element, it prevents it from submitting. If used in an anchor element, it prevents it from navigating. If used in a contextmenu, it prevents it from showing or displaying.

On the other hand, the event.stopPropagation() method is used to stop the propagation of an event or stop the event from occurring in the bubbling or capturing phase.

36) What is the real name of JavaScript?

The original name was Mocha, a name chosen by Marc Andreessen, founder of Netscape. In September of 1995, the name was changed to LiveScript. In December 1995, after receiving a trademark license from Sun, the name JavaScript was adopted.

37) How can you check if the event.preventDefault() method was used in an element?

When we use the event.defaultPrevent() method in the event object returns a Boolean indicating that the event.preventDefault() was called in a particular element.

38) What is the use of Math object in JavaScript?

The JavaScript math object provides several constants and methods to perform a mathematical operation. Unlike date object, it doesn’t have constructors.

39) What is the use of a Date object in JavaScript?

The JavaScript date object can be used to get a year, month and day. You can display a timer on the webpage by the help of JavaScript date object.

40) What is the use of a Number object in JavaScript?

The JavaScript number object enables you to represent a numeric value. It may be integer or floating-point. JavaScript number object follows the IEEE standard to represent the floating-point numbers.

41) What is the use of a Boolean object in JavaScript?

The JavaScript Boolean is an object that represents value in two states: true or false. You can create the JavaScript Boolean object by Boolean() constructor.

42) What is the use of a TypedArray object in JavaScript?

The JavaScript TypedArray object illustrates an array like a view of an underlying binary data buffer. There is any number of different global properties, whose values are TypedArray constructors for specific element types.

43) What is the distinction between the View and Session states? 

“View state” refers to a page in a session, whereas “Session state” refers to a user or browser that can access all pages in the online application. 

44) What are the different types of pop-up boxes that are available in JavaScript? 

  • Alert Box
  • Confirm Box
  • Prompt Box

45) How can we detect OS of the client machine using JavaScript?

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

46) In JavaScript, what is the purpose of the debugger keyword? 

The JavaScript debugger keyword places a breakpoint in the code. The debugger halts the program’s execution at the point where it is applied. We can now manually begin the execution flow. If an exception occurs, the execution will come to a complete stop on that line. 

47) In JavaScript, what is the purpose of a strict mode? 

Silent errors are generated using the JavaScript strict mode. To activate the strict mode, it supplies the “use strict”; phrase. Only the first statement in a script or function can use this expression.

48) How to change the background color of HTML document using JavaScript?

<script type=”text/javascript”>  



49) How to handle exceptions in JavaScript?

By the help of try/catch block, we can handle exceptions in JavaScript. JavaScript supports try, catch, finally and throw keywords for exception handling.

50) What is the debugging required in JavaScript? 

In a browser, there was no error notice for JavaScript. However, these errors may have an impact on the final product. Debugging the code is the best way to figure out what’s wrong. Web browsers such as Google Chrome and Mozilla Firebox can readily debug the code. 

Any of the following ways can be used to accomplish debugging: 

  • Using console.log() method
  • Using debugger keyword


Leave a Reply

Your email address will not be published. Required fields are marked *