1.

What are events in JavaScript?

Answer»

If you want to check the type of an object at runtime, then use the instanceof operator. The RESULT is a boolean.

Check for Array

Let’s say we are checking if a VARIABLE is an array or not using instanceof operator. The syntax:

variable_name instanceof Array

The following is our example and we are checking whether array exists or not:

 <html>    <body>         <script>         var DEPARTMENT = [ "FINANCE", "Operations", "Marketing", "HR" ];         if (department instanceof Array) {           alert('This is an Array!');         } else {           alert('This is not an array!');         }       </script>    </body> </html>

The output displays that array exists SINCE the if condition is true:

Check for Object using instanceof

To check for an object, use the instanceof operator, the following is the example:

<html>    <body>         <script>         var myObj = {};         if (myObj instanceof Object) {           alert('This is an object!');         } else {           alert('This is not an object!');         }       </script>    </body> </html>

The output displays ‘this is an object!’ in an alert box, since the if condition is true: 



Discussion

No Comment Found