[2022] JavaScript Interview Questions – Frequently Asked

Written by

Vaaruni Agarwal


Created by Brendan Eich, Java Script is a scripting language, which is called the programming language of the web. JavaScript is a high-level language, which is often compiled just-in-time, and supports multi-paradigm. JavaScript along with CSS and HTML is used to design a web page.

1) What is JavaScript?


Created by Brendan Eich, Java Script is a scripting language, which is called the programming language of the web. JavaScript is a high-level language, which is often compiled just-in-time, and supports multi-paradigm. JavaScript
along with CSS and HTML is used to design a web page.

2) What are some features of JavaScript?

Some of the features of JavaScript are:

  • Light in weight
  • Interpreted programming language
  • Good for network-centric applications
  • Complementary to HTML
  • Open source
  • Cross-platform

3) What are the advantages of JavaScript?

Some of the advantages of JavaScript are:

  • Less interaction with server, which reduces response time
  • Immediate Feedback to the visitors
  • Highly interactive
  • Provides a rich interface

4) List some of the disadvantages of JavaScript.

Some of the disadvantages of JavaScript are:

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

5) Who is the creator of JavaScript?

Brendan Eich created JavaScript.

6) Name the different types of functions available in JavaScript.

The different types of function, are:

Named Functions- These functions have a unique name. This name is given at the time of definition.

For Example:

function display()  

{  

  document.writeln(“Studymite”);  

}  

Anonymous Functions – Anonymous Functions do not need any name. They can be declared dynamically at the runtime.

var display=function()  

{  

  document.writeln(“Studymite”);  

}  

7) Define anonymous function


An anonymous function has no name in it. It is declared dynamically at runtime using the function operator. An anonymous function does not need a function declaration. The function operator is more flexible than a function
declaration. It can be easily used as an expression.

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

Yes, It is possible to assign anonymous function to a variable.

9) Define a named function in JavaScript.


A function that has a proper and unique name for itself is known as a named function in JavaScript. A named function first needs to be declared explicitly then only it can be used further. Function declaration is mandatory for a
named function and it cannot be assigned to a variable. The name given to a named function must follow all the naming rules defined by the JavaScript.

10) What is closure in JavaScript?

Closures are needed in JS, when a variable, which is defined outside the scope in reference is accessed from some inner scope. Closure helps in accessing such variables with ease.

Eg:

var n= 20;  

function sum()  

{  

document.writeln(n+n);  

}  

sum();  

11) Which method is used to return the character from a specific index?


The charAt() method is used for this purpose. This method can find out a char value present at the specified index. The index number starts from 0 and goes upto n-1, where n is the length of the string. It cannot be a negative
number, greater than or equal to the length of the string.

12) What is the difference between JavaScript and JScript?

Netscape provided the JavaScript language. However, Microsoft changed the name and called it JScript to avoid the issue of the trademark. Thus, both are same, one is provided by Microsoft and other by Netscape.

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

Any text in Java Script can be written with the help of a method called as “document.write”. It accepts string values and whatever, text is provided to it, will be printed as it is. For Example:

<script type=”text/javascript”>  

document.write(“Studymite says Hello World!”);  

</script>  

14) How to use external JavaScript file?

For using an external JavaScript file, firstly the name of the file must be known. Then with the name the files is referred with the script tag, this script tag is placed inside the head tag in HTML. For Eg:

<script type=”text/javascript” src=”studymite.js”></script>  

15) Is JavaScript a case sensitive language?

Yes, JavaScript is a case sensitive language. Thus studyMite and Studymite are two different variable names in Java Script.

16) What is BOM in Java Script?


The Browser Object Model or BOM provides the facility to interact with the browser. The default browser object is a window. So, the window functions are called by specifying the window object or directly. It has properties like
document, history, screen, navigator, location, innerHeight, innerWidth,etc.

17) What is DOM?

Document Object Model is another object model in Java Script, it is commo0nly referred as DOM. It represents the HTML document and can be used to access and change the HTML content of a page.

18) What is the use of window object?

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

19) What is the use of history object?

The history object is another object of a browser. It can be used to switch to history pages. It supports options of back and forward. The three methods of history object, are:

  • history.back()
  • history.forward()
  • history.go(number)

20) How to write a comment in JavaScript?

Unlike in HTML, 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 */

21) How to create a function in JavaScript?

For creating a function in Java Script, the use of function keyword is a must. Its syntax is as follows:

function function_name(){  

//function body  

}  

22) How many Java Script data types are there?

There are two types of data types in JavaScript:

  • Primitive Data Types , and
  • Non-primitive Data Types

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

The == operator checks equality and does not focuses on the datatype while === checks the equality, and data type also.

24) How to write HTML code dynamically using JavaScript?

In order to write the HTML code through Java Script, there is a property called the innerHTML property. It is used to write the HTML code using JavaScript dynamically. Example:

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

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

In order to write the text code through Java Script, there is a property called the innerText property. It is used to write the text code using JavaScript dynamically.

26) 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

27) 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

28) What is the isNaN() function?

It stands for is Not a Number. The isNan() function returns true if the variable value is not a number and returns false, if the returned value is a number.

function number(num) {  

  if (isNaN(num)) {  

    return “Not a Number”;  

  }  

  return “Number”;  

}  

29) What is the output of 10+20+”30″ in JavaScript?

3030

First addition of 10+20 will be 30. If there is a numeric value before and after + sign, it is treated as binary + (arithmetic operator).

30) What is the output of “10”+20+30 in JavaScript?

102030

As according to the Java Script rules after a string all the + will be treated as string concatenation operator.

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

Client-side JavaScript includes the basic language and predefined objects which run Java Script on the browser. It is embedded directly in the HTML pages. The browser interprets this script at runtime.

Server-side JavaScript also resembles client-side JavaScript. It is the JavaScript which runs in a server. The server-side JavaScript are deployed only after compilation.

32) Where are the cookies 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

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

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

34) What is the difference between undefined value and null value?

Undefined value: A value that is not defined and has no keyword is known as undefined value.  

Null value: A value that is explicitly specified by the keyword “null” is known as a null value.

If no value is given to a variable then by default it holds undefined value, that is its value is not defined, while null is itself a value.

 

35) How to set the cursor to wait in JavaScript?

The cursor can be set to wait in JavaScript by using the cursor property.

<script>  

window.document.body.style.cursor = “wait”;  

</script>  

36) How to create a three dimensional array in Java Script?

A three dimensional array can be created with the help of the[[[]]] symbol. For Example:

var myArray = [[[]]];  

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

The math object in Java Script provides a way to perform the mathematical operations. It consists of several constants and methods to perform a mathematical operation with ease.

38) What is negative infinity?

Negative Infinity is a number in JavaScript which can be derived by dividing the negative number by zero. Instead of giving out an error negative infinity is printed. For example:

var num=-5;  

function display()  

{  

  document.writeln(num/0);  

}  

//expected output: -Infinity  

39) What is the difference between View state and Session state?

View state: The view state is specific to a page in a session.

 Session State: The session state is specific to a user or browser.

40) What pop-up boxes are available in JavaScript?

Following pop – up boxes are available in Java Script

  • Alert Box        :         For warning messages
  • Confirm Box        :        For confirming user messages
  • Prompt Box        :        For taking user input.

41) How is OS detected using JavaScript?

navigator.appVersion  can be used to detect the operating system.

42) How to submit a form using JavaScript?

<form name=”myform” action=”index.php”>  

Search: <input type=’text’ name=’query’ />  

<a href=”javascript: submitform()”>Search</a>  

</form>  

<script type=”text/javascript”>  

function submitform()  

{  

  document.myform.submit();  

}  

</script>  

43) Which one is faster java Script or the ASP script?

Java Script is faster because it does not requires the web server’s support for executing itself.

44) How are exceptions handled in JavaScript?

Using the try/catch block, the exceptions can be handled easily in JavaScript. The try, catch, finally and throw keywords are used for exception handling.

46) How to validate a form in JavaScript?

<script>  

function vali (){  

var name=document.myform.name.value;  

var password=document.myform.password.value;  

 

if (name==null || name==””){  

  alert(“Enter your name please”);  

  return false;  

}else if(password.length<6){  

  alert(“Password must be at least 6 characters long.”);  

  return false;  

  }  

}  

</script>  

<body>  

<form name=”myform” method=”post” action=”abc.jsp” onsubmit=”return vali()” >  

Name: <input type=”text” name=”name”><br/>  

Password: <input type=”password” name=”password”><br/>  

<input type=”submit” value=”register”>  

</form>  

47) How to data in JavaScript?

Any validation rule can be applied over the data in Java Script with the help of the regular expressions. It is extremely simple to use and gives accurate results.

48) What is this keyword in JavaScript?

The this keyword in Java Script it a reference variable. It refers to the current object, of the class in Java Script.

 

49) What is debugging required in JavaScript?

Debugging is the process of finding errors or bugs in code, and then removing them to get the desired results. Two methods used for debugging in Java Script are:

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

50) What is debugger keyword in JavaScript?


The debugger keyword is used to set the breakpoint throughout the code. It is used for performing debugging in Java Script. It stops the execution of the program where ever it is applied. If an exception occurs, the execution will
stop again.

[2022] JavaScript Interview Questions – Frequently Asked