Javascript Interview Questions and Answers in 2022

What is the full form of ECMA?
European Computer Manufacturers Association
What is DOM?
The Document Object Model (DOM) is an application programming interface(API) for XML and HTML or fully object oriented representation of the web pages. It structure the web page as a group of nodes and objects that have properties and methods. The nodes can be removed, added, replaced, and modified easily by using the DOM API.
What is BOM?
The Browser Object Model (BOM) is consistent interface between javascript programs and web browsers. So it allows access and manipulation of the browser window. Like capacity to move, resize browser window or pop up a new browser window.
What is Javascript?
Javascript is a lightweight programming language, it is used to make most interactive and dynamic web pages. It is most widely used because it can be easily integrated with HTML and almost all popular programming languages like PHP, Java, ASP .net.
What is async attribute of script element?
The async is a boolean attribute of the script element. This is set for an external script file. It downloads the script asynchronously without delaying the page load.

<script type="text/javascript" async src="/demo.js">

How to include external JavaScript file?
We can include external JavaScript file by using the script tag

<script type="text/javascript"  src="/demo.js">
How to include JavaScript file from different server?
We can include JavaScript file from different server by specifying full script path in src attribute.
<script type="text/javascript" src="">
How to check the datatype of a variable in JavaScript?
The 'typeof' operator is used to check the datatype of a variable in JavaScript.

<script type="text/javascript">
var score= 10;
alert(typeof score); 
What is null datatype in JavaScript?
Null data type is an empty object pointer and has only one value null. The below code alert object as output.

<script type="text/javascript">
var tree = null;
alert(typeof tree);
What is undefined datatype in JavaScript?
When a variable is declared using var, but not defined, then by default it is initialized as undefined.

<script type="text/javascript">
var tree;
alert(typeof tree);
How to convert a Boolean value to number?
The Number() function is used to convert object argument to number, when we pass Boolean true and false to the Number() function, then it will return 1 and 0 respectively.

<script type="text/javascript">
var a1 = true;
var a2 = false;

alert(Number(a1)); // Output - '1'
alert(Number(a2)); // Output - '0'
What will happen when we pass a string as an argument in Number() function?
If the string is empty, then the Number() function returns 0 otherwise it returns NaN.

What is parseInt() function?
The parseInt() first examines the first character of a number, if the first character is not a number or starting with a minus or a plus sign then it returns NaN. After examining the first character it goes to rest of the characters one by one. The parseInt() function also recognizes decimal, octal and hexadecimal integers.

<script type="text/javascript">
var num1 = parseInt("8344data"); 
alert(num1); //8344 
var num2 = parseInt("434.24"); 
alert(num2); //434 
var num3 = parseInt("0xf"); 
What is parseFloat()?
The parseFloat() recognizes the floating number and decimal number format.

<script type="text/javascript">
var num1 = parseFloat("8344data"); // 8344
var num2 = parseFloat("434.24"); //434 
var num3 = parseFloat("0xA"); //0
How to convert a value to a string?
By using the toString() method, we can convert a value to a string. This method returns string equivalent to a value.

var num =10;
alert(num.toString()); //10 
alert(num.toString(2)); //1010
alert(num.toString(8)); //12
alert(num.toString(10)); //10
alert(num.toString(16)); //a

Is it possible to overload function in JavaScript?
No, In JavaScript, the function overloading is impossible.
What is the difference between Primitive Data Type and Reference Data Type?
Number, String, Boolean, undefined, null are Primitive Data Types. Primitive values are stored on stack, whereas Object, Array are Reference Data Types. The reference values are objects made up of multiple values and stored in the heap.
Give some examples of script element attributes.
These are attributes of script element.
a) async, b) charset, c) defer, d) language, e) src, f) type
What is the use of src attribute of the script element?
The src attribute is used to include the external javascript file that is either located on the same server or different server.

<script type="text/javascript" async src="/demo.js">

Define Global Variable in JavaScript?
A global variable has global scope. It is declared outside a function and can be accessed anywhere in the JavaScript code.
<script type="text/javascript">
var x1 = 10;
function sample(){
  alert(x1); // Output: 10
 alert(x1); // Output: 10
What is Anonymous Functions?
The Anonymous function called using the variable name. To create a anonymous function, we do not need a function name. This functions are created at runtime.
var sum = function(a,b){return a+b;};   
What are the Event Handlers in Javascript?
Event Handler invokes Javascript code when some event or action happends, such as on mouse click, mouse hover over some link.

What is return statement in Javascript?
The return statement is used inside a function to returns a value and exits from the current function.
<script type="text/javascript">
function sample(){
  var x1 = 10;
  var x2 = 20;
  var sum = x1 + x2;
  return sum;
var y = sample(); 
alert(y); // Output: 30
Define const statement?
The const statement declares a variable with a constant value. The const variable can be local or global. If it is declared within a function then its scope remain within that function block only.
What is try.. catch block?
The try...catch block provides to handle errors. The JavaScript code that may produce an error is written within try block. If an error occurs in the try block, then the program control is passed to catch block. If no error occurs, the program control is never pass to catch block. The catch block contain statements to handle the exception.

<script type="text/javascript">
try {
 addnumber(x1, x2);
catch(err) {
 var error_msg = err.message;
 alert(error_msg); //Output: addnumber is not defined
What is 'this' keyword in Javascript?
The 'this' keyword is used to access the current object's properties. If we pass this on some event or function call, then we can access all the properties of that element within the function.

What is Math Object in javascript?
Javascript has Math Object to perform mathematical operations on numbers. Math Object has several methods to handle different mathematical tasks.

<script type="text/javascript">
var x1 = Math.round(98.2); // It returns round figure of the number

var x2 = Math.random()  // It returns random number
How to get the Current Date in javascript?
The getDate() method is used to get the current date. But for this, first we have to create object of Date() constructor.

<script type="text/javascript">
var d = new Date();
var currentdate = d.getDate();
What is Document Object?
Everything on the web page is document object that is loaded in the browser window. Javascript call methods of the document object to work on the web page.

<script type="text/javascript">
document.write('Hello World');
What is Window object?
Window Object represents a browser window containing a DOM document. We can use it to set attributes for the browser window. For example, to set status bar message, we can use 'status' window property.

Give some examples of window methods.
These are the examples of window methods -
alert(), open(), close(), prompt(), confirm()
How you will insert JavaScript code in HTML page?
We can insert JavaScript code in HTML page by the following way -
<script>document.write('Hello World');</script>

What is isNAN()?
The isNAN() function returns true if the value pass in this function is not a number otherwise it returns false.
How you will create object in JavaScript?
We can create object in javascript by using new operator followed by the name of the object.

Example - obj = new Object();
What is Logical NOT operator?
Logical NOT operator represented by exclamation mark (!), it returns boolean value. First it converts the value to boolean value then negates it.

Example -

Define do-while loop?
Do while loop is almost similar to the while loop except that the loop statement is executed at least once. In this, the conditional check is written at the end of the loop iteration. So the statement is executed first before the condition is tested.
<script type="text/javascript">
var i = 0;
do { 
    document.write("Number : " + i + "<br/>");
while (i < 4); 

Define Break Statement?
The break statement is placed inside the loop to terminate the iteration and start to execute the code immediately after the loop.
<script type="text/javascript">
for(var i = 1; i< 10; i++) {
    if(i == 5)
    document.write("Number : " + i + "<br/>");
document.write("Loop terminates");

Define continue statement?
The continue statement is used inside a loop. When continue statement is executed, it stops the current iteration of the loop and continue with the next iteration of the loop.
<script type="text/javascript">
for(var i = 1; i< 10; i++) {
    if(i == 5) 
    document.write("Number : " + i + "<br/>");

What is an array?
An array is a collection of key/value pairs. We can store more than one item in a single variable. i.e. Array. So Array is used when there is requirement to add more items in a single variable.
Is JavaScript Garbage Collection language?
Yes, JavaScript is a Garbage Collection Language, it automatically manage the memory allocation. It keeps track of variables that are in use or not in use and allocate the memory according to the requirement.
How will you reverse the array element in JavaScript?

By using the reverse() method, we can reverse the elements order of an array.
var elements = [11,33,22, 55, 77];
alert(elements); //77,55,22,33,11
Define forEach() method?
This method is used to run function on every element of an array.
How will you get minimum and maximum values in a range in Javascript?
By using Math min(), max() methods, we can find the minimum and maximum value in a range-
<script type="text/javascript">
 var max = Math.max(12, 32, 2, 13);
 alert(max); //32
 var min = Math.min(12, 32, 2, 13);
 alert(min); //2
What is a prompt box?
Prompt box is a method that pops up on browser's screen for user input. It contains some confirmation message or any kind of input from user.
How can you insert HTML code dynamically using JavaScript?
We can insert HTML code dynamically using InnerHTML property.

document.getElementById('txt').innerHTML() = "<h1>Hello World!</h1>";
How do you join two or more arrays?
We can join two or more than two arrays by using the concat() method. This function returns a copy of the joined arrays.

Syntax of concat() method -
arrayObject.concat(array1, array2, array3, ........, arrayX);
What is the use of parse() method in Javascript?
The parse() method takes a date string and returns the number of milliseconds since midnight of January 1, 1970.

<script type="text/javascript">
var d = Date.parse("Jul 11, 2012");
What is the method to get random number in Javascript?
The random() method is used to get random number between 0 and 1.

What is Regular Expression?
Regular Expression is a sequence of characters that form a search pattern. This is used to perform pattern matching and search-and-replace functions on text.
RegExp constructor is used to create a regular expression object.
How can you print a webpage using JavaScript?
We can print a webpage by using the window.print() function.

	<input type="button" value="Print Page" onclick="window.print()" />

How JavaScript codes get executed?
JavaScript is scripting language. JavaScript codes reside on the user's browser and interpreted and executed within the browser.

JavaScript Related Articles

Remove duplicates from array JavaScript
How to reverse a number in JavaScript
How to reverse string in JavaScript
JavaScript display PDF in the browser using Ajax call
Parsing JSON in Javascript
Javascript speech recognition example
Select/deselect all checkboxes using Javascript
Print specific part of a web page in javascript
Dynamically Add/Delete HTML Table Rows Using Javascript
jQuery Ajax serialize form data example
Image popup on page load using HTML and jQuery
Ajax live data search using jQuery PHP MySQL
jQuery loop over JSON result after AJAX Success
Simple star rating system using PHP, jQuery and Ajax
jquery image zoom on mouseover example
Bootstrap star rating input example
Bootstrap datepicker example
Submit a form data without page refresh

Read more articles

General Knowledge

Learn Popular Language