JavaScript Interview Questions

Level: Freshers

Q 21 - What are the different types of errors in JavaScript?

There are three types of errors:
  •     Load time errors: Errors which come up when loading a web page like improper syntax errors are known as Load time errors and it generates the errors dynamically.
  •     Run time errors: Errors that come due to misuse of the command inside the HTML language.
  •     Logical Errors: These are the errors that occur due to the bad logic performed on a function which is having different operation.

Q 22 - Explain window.onload and onDocumentReady?

  • The onload function is not run until all the information on the page is loaded. This leads to a substantial delay before any code is executed.
  •  onDocumentReady loads the code just after the DOM is loaded. This allows early manipulation of the code.

Q 23 - What is the difference between .call() and .apply()?

  • The function .call() and .apply() are very similar in their usage except a little difference. .call() is used when the number of the function’s arguments are known to the programmer, as they have to be mentioned as arguments in the call statement. On the other hand, .apply() is used when the number is not known. The function .apply() expects the argument to be an array.
  •  The basic difference between .call() and .apply() is in the way arguments are passed to the function. Their usage can be illustrated by the given example
Example: <br /> var someObject = {<br /> &nbsp;<br /> myProperty : &#39;Foo&#39;,<br /> &nbsp;<br /> myMethod : function(prefix, postfix) {<br /> &nbsp;<br /> alert(prefix + this.myProperty + postfix);<br /> &nbsp;<br /> }<br /> &nbsp;<br /> };<br /> &nbsp;<br /> someObject.myMethod(&#39;&lt;&#39;, &#39;&gt;&#39;); // alerts &#39;&lt;Foo&gt;&#39;<br /> &nbsp;<br /> var someOtherObject &nbsp;= {<br /> &nbsp;<br /> myProperty : &#39;Bar&#39;<br /> &nbsp;<br /> };<br /> &nbsp;<br />, &#39;&lt;&#39;, &#39;&gt;&#39;); // alerts &#39;&lt;Bar&gt;&#39;<br /> &nbsp;<br /> someObject.myMethod.apply(someOtherObject, [&#39;&lt;&#39;, &#39;&gt;&#39;]); // alerts &#39;&lt;Bar&gt;

Q 24 - What boolean operators can be used in JavaScript?

The ‘And’ Operator (&&), ‘Or’  Operator (||) and the ‘Not’ Operator (!) can be used in JavaScript. *Operators are without the parenthesis.

Q 25 - How are DOM utilized in JavaScript?

DOM stands for Document Object Model and is responsible for how various objects in a document interact with each other. DOM is required for developing web pages, which includes objects like paragraph, links, etc. These objects can be operated to include actions like add or delete. DOM is also required to add extra capabilities to a web page. On top of that, the use of API gives an advantage over other existing models.