Online Tutorials & Training Materials |
Register Login

Advanced JavaScript Interview Questions and Answers

21 Nov 2018 11:23 am || 0

1. How to validate email addresses in JavaScript?

The most used method for validating email addresses in JavaScript encompasses the Regular Expression. As there exists no generic/ regular email check regex, coders are found using different ones to suit their purpose. It is best to opt for a Regular Expression that can calculate the new domains that have been introduced as well as validate all kinds of internationalized email addresses.

2. How to add JavaScript in HTML?

A JavaScript code can be added in an HTML document through the dedicated HTML tag called <script>; this HTML tag wraps itself around the JavaScript code.

3. How to redirect to another page in JavaScript?

Users can be redirected to another URL coded in JavaScript once a specific website has been opened. In order to do so, the code given below can be included at the top of the website page from which the redirection will take place. This code is to be incorporated in the <head> element. In case a separate.js file is being used, then the code has to be copied into the file and linked from the <head> of the website page having the redirection command. 

window.location.href = "";

The target URL that the website page has to be redirected to should be included in the above code. 

4. How to call a function in JavaScript?

The pre-defined call() method in JavaScript can be used for the purpose of invoking (calling) a method that contains an owner object as its argument (parameter). The call() allows objects to use the methods belonging to other objects as well. 

5. How to declare an array in JavaScript?

The array literal given below allows coders to define an array:

var x = [];
var y = [1, 2, 3, 4, 5];

6. How to debug JavaScript?

The latest released versions of JavaScript have exception handling capabilities that have been newly added. JavaScript implements the construct related to ‘try...catch...finally’, along with the throw operator, for the cause of handling exceptions. JavaScript syntax errors cannot be caught; however, the runtime exceptions and programmer-generated exceptions are capable of being traced and debugged. 

7. What is an object in JavaScript and how do coders create an object in JavaScript?

JavaScript has been created with a simple object-based paradigm as its base. In JS, an object serves as a collection of properties. The association between a value and a name (or key) is referred to as a property. The value assigned to a property can also be a function; in such cases, the property will be called a function. The object concept is well-supported by JavaScript. An object can be created with the help of an object literal as in the following example:

var emp = {
   name: "Zara",
   age: 10

8. What is closure in JavaScript?

A closure is created in JavaScript in the event of a variable defined (from outside the current scope) has to be accessed by the program from within any inner scope. 

9. How to convert string to int in JavaScript?

The parseInt () method is used for converting strings into integers (whole numbers). 

parseInt() #

It accepts 2 arguments. The argument that it takes, in the beginning, is the string that has to be converted.  The next argument is referred to as the radix, which is the base number put to use in mathematical systems. In this case, it carries a value of 10 at all times. The following example depicts the conversion of a string to an integer:

var text = '42px';
var integer = parseInt(text, 10);
// returns 42

10. How to get the value of a textbox in JavaScript?

Assume that the attribute id="myTextBox" has been assigned in HTML:

<input type="text" id="myTextBox">

The value of the textbox can be attained in JavaScript by using the following code:

myTextBox = document.getElementById("myTextBox");
value = myTextBox.value;

JavaScript Interview Questions and Answers

11. What is promise in JavaScript?

A promise refers to the eventual result thrown back by an asynchronous operation. It is in the nature of a placeholder which helps in materializing the reason – or failure or the successful result value.

12. How to compare two strings in JavaScript?

The localeCompare() method is put to use for comparing two strings in JavaScript. This method will return the 0 value in case both the strings happen to be equal; -1 in case string 1 has been sorted out before string 2; and 1 if the sorting of string 2 takes place before string 1. 

13. How to call JavaScript function in PHP?

The following code snippet is used for calling the JavaScript function from PHP:

    function a () {
        alert(“This is msg.”);

    $a = 1;
    if ($a == 2) {
    } else {
        echo ‘<script>’,

14. How to reverse a string in JavaScript?

The reverse() method is used for reversing the order of all elements contained in an array – the last element comes to the first place and vice versa. 

15. How to hide div in JavaScript?

The following JavaScript code snippet is used for hiding div in JavaScript:

    function showhide()
         var div = document.getElementById(“newpost”);  
         if ( !== “none”) 
    = “none”;  
    = “block”;  

16. What is strict mode in JavaScript?

The Strict Mode serves to be a new feature in ECMAScript 5. It allows JavaScript to add a function or a program in the “strict” operating context. The context will restrict certain actions for taking place and also throws back more exceptions.

17. How to minify JavaScript?

UglifyJS can be used to minify JavaScript. Additionally, the Closure Compiler also serves to be an effective feature for this purpose. It is possible to generate a build process for the sake of using these tools to minify as well as rename the development files. The renamed files can then be saved to a production directory.

18. How to create JSON object in JavaScript?

The creation of an empty JSON object takes place with the following code:

var JSONObj = {};

Creation of a new object would require the following code snippet:

var JSONObj = new Object();

For instance, the creation of a JSON object having the attribute ‘bookname’ with string value, and the attribute price with a numeric value, can be done as given below:

var JSONObj = {“bookname ":"VB BLACK BOOK", "price":500};

19. What is prototype in JavaScript?

When functions are created in JavaScript then the JavaScript Engine becomes responsible for adding the prototype property to that specific function. Also referred to as prototype object, the prototype property is in the nature of an object () that depicts the constructor property. The constructor property is designed to point back to the very function that has the prototype object as a property.

20. How to add multiple markers on Google Maps in JavaScript?

The following code snippet is an example of adding multiple markers on Google Maps in JavaScript:

var beaches = [
    ['Bondi Beach', -33.890542, 151.274856, 4],
    ['Coogee Beach', -33.923036, 151.259052, 5],
    ['Cronulla Beach', -34.028249, 151.157507, 3],
    ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
    ['Maroubra Beach', -33.950198, 151.259302, 1]

for (var i=0;i<beaches.length;i++)
    // add your markers here

Multiple markers may be appended by populating an array that contains all locations. The next step requires iterating through the array for adding each marker onto the Google Map. 

21. How to add class in JavaScript?

Call for the <div> element that has the ID ="myDIV"; next, add the "mystyle" class to the same. 

The following code snippet example will help you add a class in JavaScript:

function myFunction() {
    var element = document.getElementById("myDIV");