Library

Real-time JavaScript Interview Questions and Answers – Simpliv

Real-time JavaScript Interview Questions and Answers – Simpliv

JavaScript interviews are formed to test the JavaScript skills of a candidate. Our aim is to help you understand how to face JavaScript and make it less daunting than it appears to be. Our guidance is designed to help you overcome the hurdles of facing a technical JavaScript round and a tough HR call. This article is aimed at helping you succeed at these rounds.

Our questions are based on our experience of having conducted and observed other technical and HR professionals conduct these rounds. Our guide is aimed at helping you answer JavaScript and HR rounds from beginner to advanced. We hope you will find this part useful. We wish you the best!

1ST Round: JavaScript Basic interview questions and answers:

1.Define JavaScript

Ans: JavaScript is a scripting language, as well as an Object based Programming language. Its specialty is that it can be used on both client-side as well as server-side and can be inserted into HTML pages. JavaScript is a language that can be understood by web browsers.

2.How do you differentiate between Java and JavaScript?

Ans: To start with, while Java is a complete programming language, JavaScript is a coded program that can be introduced to HTML pages. One other fundamental difference between java and JavaScript is that Java is an object - oriented programming or structured programming language like C++ or C, while JavaScript is a client-side scripting language. Although they sound similar, the two are not interdependent. They are both designed for different and specific purposes.

3.Which are the JavaScript Data Types?

Ans: These are the JavaScript Data types:

  1. Number
  2. String
  3. Boolean
  4. Function
  5. Object
  6. Undefined

4. What purpose does isNaN function serve?

Ans: This is what the isNan function does: It returns true if the argument is not a number. Else,  it is false.

5. Define negative infinity

Ans: A number in JavaScript, Negative Infinity is derived by dividing any negative number by zero.

6. Which is the company that is credited with having developed JavaScript?

Ans: It is Netscape.

7. How do you define undeclared and undefined variables?

Ans: Undeclared variables: These are variables that do not exist or declared in a program. a runtime error props up whenever the program tries to read the value of an undeclared variable.

On the other hand, variables that are declared in the program but without being given any values is an undefined variable. So, whenever the program tries to read the value of an undefined variable, what is returned is an undefined value.

8. How do you understand a prompt box?

Ans: It is a box which allows the user to enter input, for which it provides a text box. It offers label and box to enter the text or number.

9. Define 'this' keyword in JavaScript

Ans: 'This' keyword is the object from where it was called.

10. How do you read and write a file using JavaScript?

Reading and writing a file using JavaScript can be done in two ways:

  1. By using JavaScript extensions
  2. By using a web page and Active X objects

11. What result would you get of 3+2+"7"?

Ans: In JavaScript, as 3 and 2 are integers, they will be added numerically. 7 being a string, JavaScript does its concatenation. So, the answer is 57.

12. How do you detect the operating system on the client machine?

Ans: This is done by using the navigator.platform string (property).

13. How do you explain an undefined value in JavaScript?

Ans: In JavaScript, an undefined value means:

  1. The variable used in the code doesn't exist
  2. The variable is not assigned to any value
  3. The property doesn't exist.

14. What use does Void(0) have?

Ans: Void(0) serves this purpose: preventing the page from refreshing and passing parameter “zero” while calling.

Using Void(0), another method can be called without refreshing the page.

15. What are break and continue statements?

Ans: While break statement exits from the current loop; continue statement continues with the next statement of the loop.

16. What use does ‘Typeof’ operator have?

Ans: 'Typeof' is an operator used to return a string description of the type of a variable.

17. What purpose does the blur function have?

Ans: When you are required to remove the focus from the specified object, you use the blur function.

18. How do you differentiate between JavaScript and Jscript?

Ans: The two are essentially the same, with the only difference being that while Netscape developed JavaScript; Microsoft developed Jscript.

19. Define the unshift method in JavaScript

Ans: One can liken Unshift method to push method which works at the beginning of the array. You use this method to prepend one or more elements to the beginning of the array.

20. What are window.onload and onDocumentReady?

Ans: window.onload function is run only after all the information on the page is loaded. Naturally, there is quite a delay before any code gets executed.

On the other hand, in loading the code just after the DOM is loaded; onDocumentReady facilitates early manipulation of the code.

21. What role do break and continue statements have?

Ans: While Break statement is used to come out of the current loop; the continue statement continues the current loop with a new recurrence.

22. What are the ways by which JavaScript and ECMA Script are related?

Ans: One can understand ECMA Script as being a set of rules and guidelines, while JavaScript is a scripting language that is used for web development.

2nd Round: JavaScript Technical Interview Questions and answers:

1. Differentiate between undefined and not defined in JavaScript?

Ans: Should you try to use a variable that doesn't exist and has not been declared, then JavaScript will throw an error var name is not defined. This action stops executing the script. However, if you use typeof undeclared_variable, then it will return undefined.

An understanding of the difference between declaration and definition will give us clarity on these functions:

Assume that var x is a declaration because you have not defined what value it holds yet, but you have declared its existence and the need for memory allocation.

> var x; // declaring x
> console.log(x); //output: undefined 

Here var x = 1 is both a declaration and definition (We can also say we are doing an initialization. In the example above, the declaration and assignment of value happen inline for variable x. In JavaScript, every variable or function declaration you bring to the top of its current scope is called hoisting.

The assignment happens in order, so when we try to access a variable that is declared but not defined yet, we will get the result undefined.

var x; // Declaration
if(typeof x === 'undefined') // Will return true

With a variable that is neither declared nor defined, when we try to reference such a variable, the result we get is not defined.

> console.log(y);  // Output: ReferenceError: y is not defined

2. When the code below is executed, in what order will the numbers 1-4 be logged to the console? Why?

Ans: (function() {
    console.log(1); 
    setTimeout(function(){console.log(2)}, 1000); 
    setTimeout(function(){console.log(3)}, 0); 
    console.log(4);
})();
 

3. What will the code below output to the console be, and why?

 
Ans: var arr1 = "jack".split('');
var arr2 = arr1.reverse();
var arr3 = "joan".split('');
arr2.push(arr3);
console.log("array 1: length=" + arr1.length + " last=" + arr1.slice(-1));
console.log("array 2: length=" + arr2.length + " last=" + arr2.slice(-1));

4. What is the unshift() method?

Ans: Unlike the push(), this method is functional at the starting of the array. It adds the desired number of elements to the top of an array. For example -

var name = [ "Thomas" ];
name.unshift( "John" );
name.unshift( "Trevor", "Mary" );
console.log(name);

The output is shown below:

["Thomas"," John", " Trevor", " Mary"]
 

5. Describe decodeURI() and encodeURI()?

Ans: While encodeURl() is used to convert URL into their hex coding; decodeURI() is used to convert the encoded URL back to normal.

<script>
               var uri="my test.asp?name=ståle&car=saab";
 
               document.write(encodeURI(uri)+ "<br>");
 
               document.write(decodeURI(uri));
</script>

Output -

my%20test.asp?name=st%C3%A5le&car=saab

my test.asp?name=ståle&car=saab

6. How do you hide JavaScript codes from old browsers that don't support JavaScript?

Ans: This is how JavaScript codes are hidden from old browsers:

Add "<!--" without the quotes in the code just after the <script> tag.

Add "//-->" without the quotes in the code just before the <script> tag.

Old browsers will now treat this JavaScript code as a long HTML comment. While, a browser that supports JavaScript, will take the "<!--" and "//-->" as one-line comments.

7. Explain a “closure” in JavaScript with an example?

Ans: A closure is a function defined inside another function called the parent function. A closure has access to variables that are declared and defined in the parent function scope.

The closure has access to variables in three scopes:

·                    Variables declared in their own scope

·                    Variables declared in a parent function scope

·                    Variables declared in the global namespace

var globalVar = "abc"; 
 
// Parent self invoking function 
(function outerFunction (outerArg) { // begin of scope outerFunction
    // Variable declared in outerFunction function scope 
    var outerFuncVar = 'x';    
    // Closure self-invoking function 
    (function innerFunction (innerArg) { // begin of scope innerFunction
        // variable declared in innerFunction function scope
        var innerFuncVar = "y"; 
        console.log(          
            "outerArg = " + outerArg + "\n" +
            "outerFuncVar = " + outerFuncVar + "\n" +
            "innerArg = " + innerArg + "\n" +
            "innerFuncVar = " + innerFuncVar + "\n" +
            "globalVar = " + globalVar);
 
    }// end of scope innerFunction)(5); // Pass 5 as parameter 
}// end of scope outerFunction )(7); // Pass 7 as parameter 

innerFunction is a closure that is defined inside outerFunction and has access to all variables declared and defined in the outerFunction scope. In addition, the function defined inside another function as a closure will have access to variables declared in the global namespace.

Hence, the output of the code above would be:

outerArg = 7
outerFuncVar = x
innerArg = 5
innerFuncVar = y
globalVar = abc

8. What is undefined x 1 in JavaScript?

Ans: var trees = ["redwood","bay","cedar","oak","maple"];
delete trees[3];

When the code above is run and you type console.log(trees); into your Chrome developer console, you will get

["redwood", "bay", "cedar", undefined × 1, "maple"]. When you run the code in Firefox's browser console, you will get ["redwood", "bay", "cedar", undefined, "maple"]. It is thus clear that the Chrome browser has its own way of displaying uninitialized indexes in arrays. However, when you check trees[3] === undefined in both browsers, you will get similar output as true.

Note: Please remember you do not need to check for the uninitialized index of array in trees[3] === 'undefined × 1', as it will give you an error. 'undefined × 1' is just way of displaying an array's uninitialized index in Chrome.

9. If we have a JavaScript associative array?

Ans: var counterArray = {
    A : 3,
                B : 4
};
counterArray["C"] = 1;

10. What is function hoisting in JavaScript?

Ans: Function Expression

var foo = function foo(){ 
               return 12; 
}; 

Variables and functions are hoisted in JavaScript. Looking at function hoisting first, the JavaScript interpreter looks ahead to find all variable declarations and then hoists them to the top of the function where they're declared. For example:

foo(); // Here foo is still undefined 
var foo = function foo(){ 
               return 12; 
}; 

Behind the scene of the code above looks like this:

var foo = undefined;
    foo(); // Here foo is undefined 
                  foo = function foo(){
                     / Some code stuff
      }
var foo = undefined;
                foo = function foo(){
                    / Some code stuff
    }
    foo(); // Now foo is defined here
 
 

HR Round:

What has been your experience of having worked with JavaScript?

Can you give references to the projects you completed involving JavaScript?

Can you mention any prominent challenge you faced while implementing a JavaScript project? How did you overcome it?

Have you missed a deadline for any project involving JavaScript at your current employment? What was the reason for it and how did you manage it?

Want to triumph at your technical and HR rounds at your JavaScript interviews? Our aim is to help you overcome the jitters of having to face these rounds at your interview for JavaScript developer. We want to make sure you don’t miss out on the interview, having put in so much to get there and having spent such a good part of your career in this field. We have framed our set of questions from our experience in having helped candidates like you come up with the right smart answers and pass through these rounds with confidence. All the best to you!