JavaScript Interview Questions

aimtocode

Q 1) What is JavaScript?

JavaScript is a scripting language developed by Netscape. It can be used to program web browser or even servers. It can dynamically update the contents of the webpage, which is the beauty of JavaScript.

Q 2) What are the advantages of using External JavaScript?

Using External JavaScript in our code has many advantages as stated below.

  • Separation of Code is done.
  • Code Maintainability is Easy.
  • Performance is better.

Q 3) In the following Code snippet can you please predict the output or If you get an error, please explain the error?

<!DOCTYPE html>
<html>
<body>
<h2> <strong> Sample: Software Testing Help</strong> </h2>
<p id="studentName"></p>

<script>
var studentName = "Prayag Verma"; // String 'Prayag Verma' stored in studentName
var studentName; // varaible is decalred again
document.getElementById("studentName").innerHTML = 
"Redeclaring the varaible will not lose the value!.<br>"
+"Here the value in studentName is "+ studentName;

</script>

</body>
</html>

This code will not produce any errors.

Redeclaration of the variables is allowed in JavaScript. Hence, the value of the variable will not be lost after the execution of the statement here.

Q 4) What will the code below output to the console and why?

 var myObject = {
    foo: "bar",
    func: function() {
        var self = this;
        console.log("outer func:  this.foo = " + this.foo);
        console.log("outer func:  self.foo = " + self.foo);
        (function() {
            console.log("inner func:  this.foo = " + this.foo);
            console.log("inner func:  self.foo = " + self.foo);
        }());
    }
 };
 myObject.func();

In the outer function, both this and self refer to myObject and therefore both can properly reference and access foo.

In the inner function, though, this no longer refers to myObject. As a result, this.foo is undefined in the inner function, whereas the reference to the local variable self remains in scope and is accessible there.

Q 5) 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

Q 6) 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.

Q 7) Define a named function in JavaScript.

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

    function msg()  
    {  
      document.writeln("Named Function");  
    }  
    msg();  
    

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

 var display=function()  
 {  
   alert("Anonymous Function is invoked");  
 }  
 display(); 

Q 9) 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

Q 10) How to write normal text code using JavaScript dynamically?

The innerText property is used to write the simple text using JavaScript dynamically. Let's see a simple example:

 document.getElementById('mylocation').innerText="This is text using JavaScript";   

Q 11) Difference between Client side JavaScript and Server side JavaScript?

  • Client-side They run on a browser (front end). This environment is known as Client side scripting environment.
  • The processing of the scripts takes place on the end users computer.

  • Server-side scripting Languages are run on a web server (back end). This environmet is known as Server side scripting environment.
  • The user issues a request and it is fulfilled by running a script, directly on the web server.
  • Q 12) How to set the cursor to wait in JavaScript?

    The cursor can be set to wait in JavaScript by using the property "cursor". The following example illustrates the usage:

     <script>  
      window.document.body.style.cursor = "wait";   
     </script>
    
aimtocode