Javascript Interview Questions and Answers 2019

1.
What is the full form of ECMA?
European Computer Manufacturers Association  
2.
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.
3.
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.
4.
What is Javascript?
Javascript is most popular scripting language that executes within a browser. It is used to make the webpages more interactive. The Javascript code is embedded within an HTML document.
5.
What is async attribute of script element?
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">
6.
How to include external javascript file?
We can include external javascript file by using script tag

<script type="text/javascript"  src="/demo.js">
7.
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="https://www.xyz.com/demo.js">
8.
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); 
</script>
9.
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);
</script>
10.
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);
</script>
11.
How to convert a Boolean value to number?
Number() function is used to convert object argument to number, when we pass Boolean true and false to 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'
</script>
12.
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.
13.
What is parseInt() function?
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"); 
alert(num3);//15
</script>
14.
What is parseFloat()?
parseFloat() recognizes the floating number and decimal number format.

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

<script>
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”
</script>
16.
Is it possible to overload function in javascript?
No, javascript function overloading is impossible.
17.
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.
18.
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
19.
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">
20.
Define Global Variable in javascript?
A global variable has global scope. It is declared outside a function and can be accessed anywhere in javascript code.
<script type="text/javascript">
var x1 = 10;
function sample(){
  alert(x1); // Output: 10
}
 alert(x1); // Output: 10
</script>
21.
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;};   
sum();
22.
What are the Event Handlers in Javascript?
Event Handler invokes Javascript code when some event or action happeds, such as on mouse click, mouse hover over some link.
23.
What is return statement in Javascript?
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
</script>
24.
Define const statement?
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.
25.
What is try.. catch block?
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
}
</script>
26.
What is 'this' keyword in Javascript?
'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.
27.
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
alert(x1);

var x2 = Math.random()  // It returns random number
alert(x2);
</script>
28.
How to get the Current Date in javascript?
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();
alert(currentdate);
</script>
29.
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');
</script>
30.
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.
31.
Give some examples of window methods.
These are the examples of window methods -
alert(), open(), close(), prompt(), confirm()
32.
How you will insert javascript code in HTML page?
We can insert javascript code in HTML page by the following way -
<html> 
<head> 
<script>document.write('Hello World');</script>
</head>
</html>

33.
What is isNAN()?
isNAN() function returns true if the value pass in this function is not a number otherwise it returns false.
34.
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();
35.
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 -
alert(!(true));
alert(!(1000));
36.
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/>");
    i++;
} 
while (i < 4); 
</script>

37.
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)
    { 
        break; 
    }
    document.write("Number : " + i + "<br/>");
}
document.write("Loop terminates");
</script>

38.
Define continue statement?
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) 
    { 
        continue; 
    }
    document.write("Number : " + i + "<br/>");
}
</script>

39.
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.
40.
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.
41.
How will you reverse the array element in javascript?
By using reverse() method, we can reverse the elements order of an array.

var elements = [11,33,22, 55, 77];
elements.reverse();
alert(elements); //77,55,22,33,11

42.
Define forEach() method?
This method is used to run function on every element of an array.
43.
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
</script>

44.
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.
45.
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>";
46.
How do you join two or more arrays?
We can join two or more than two arrays by using concat() method. This function returns a copy of the joined arrays.

Syntax of concat() method -
arrayObject.concat(array1, array2, array3, ........, arrayX);
47.
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");
document.write(d);
</script>
48.
What is the method to get random number in Javascript?
random() method is used to get random number between 0 and 1.

alert(Math.random());
49.
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.
50.
How can you print a webpage using Javascript?
We can print a webpage by using window.print() function.

<form>
	<input type="button" value="Print Page" onclick="window.print()" />
</form>
51.
How Javascript codes get executed?
Javascript is scripting language. Javascript codes reside on the user's browser and interpreted and executed within the browser.




Read more articles