JavaScript Interview Questions

JavaScript Interview Questions

1) What is JavaScript?

JavaScript is a scripting language. It is different from Java language. It is object-based, lightweight, cross-platform translated language. It is widely used for client-side validation. The JavaScript Translator (embedded in the browser) is responsible for translating the JavaScript code for the web browser. More details.

2) List some features of JavaScript.

Some of the features of JavaScript are:

  • Lightweight
  • Interpreted programming language
  • Good for the applications which are network-centric
  • Complementary to Java
  • Complementary to HTML
  • Open source
  • Cross-platform

3) Who developed JavaScript, and what was the first name of JavaScript?

JavaScript was developed by Brendan Eich, who was a Netscape programmer. Brendan Eich developed this new scripting language in just ten days in the year September 1995. At the time of its launch, JavaScript was initially called Mocha. After that, it was called Live Script and later known as JavaScript.

4) List some of the advantages of JavaScript.

Some of the advantages of JavaScript are:

  • Server interaction is less
  • Feedback to the visitors is immediate
  • Interactivity is high
  • Interfaces are richer

5) List some of the disadvantages of JavaScript.

Some of the disadvantages of JavaScript are:

  • No support for multithreading
  • No support for multiprocessing
  • Reading and writing of files is not allowed
  • No support for networking applications.

6) Define a named function in JavaScript.

The function which has named at the time of definition is called a named function. For example

  1. function msg()  
  2. {  
  3.   document.writeln(“Named Function”);  
  4. }  
  5. msg();  

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:
    1. function display()  
    2. {  
    3.   document.writeln(“Named Function”);  
    4. }  
    5. display();  
  • Anonymous – These type of functions doesn’t contain any name. They are declared dynamically at runtime.
    1. var display=function()  
    2. {  
    3.   document.writeln(“Anonymous Function”);  
    4. }  
    5. display();  

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:

  1. var display=function()  
  2. {  
  3.   alert(“Anonymous Function is invoked”);  
  4. }  
  5. display();  

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.

  1. var num = 10;  
  2. function sum()   
  3. {  
  4. document.writeln(num+num);  
  5. }   
  6. 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:

  1. var str=“Javatpoint”;    
  2. document.writeln(str.charAt(4));    

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.

  1. <script type=“text/javascript”>  
  2. document.write(“JavaScript Hello World!”);  
  3. </script>  

More details.

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

JavaScript is a lightweight programming language (most commonly known as scripting language) developed by Netscape, Inc. It is used to make web pages interactive. It is not a part of the Java platform. Following is a list of some key differences between Java and JavaScript

A list of key differences between Java and JavaScript


Java 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.
Java is a Complete and Standalone language that can be used in backend coding. JavaScript is assigned within a web page and integrates with its HTML content.
Java programs consume more memory. JavaScript code is used in HTML web pages and requires less memory.
The file extension of the Java program is written as “.Java” and it translates source code into bytecodes which are then executed by JVM (Java Virtual Machine). The JavaScript file extension is written as “.js” and it is interpreted but not compiled. Every browser has a JavaScript interpreter to execute the JS code.
Java supports multithreading. JavaScript doesn’t support multithreading.
Java uses a thread-based approach to concurrency. JavaScript uses an event-based approach to concurrency.

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.

  1. <script type=“text/javascript” src=“message.js”></script>  

More details.

17) Is JavaScript case sensitive language?

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

  1. Var msg = “JavaScript is a case-sensitive language”; //Here, var should be used to declare a variable  
  2. function display()   
  3. {  
  4. document.writeln(msg); // It will not display the result.  
  5. }   
  6. display();  

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,

javascript object model More Details: Browser Object Model

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.

More Details: Document Object Model

20) What is the use of window object?

The window object is created automatically by the browser that represents a window of a browser. It is not an object of JavaScript. It is a browser object.


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

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

Shubham Patil

Typically replies within a day

Powered by WpChatPlugins

Learn to code by doing. Try hands-on Coding with RLchats PRO.