JavaScript | getPrototypeOf() with Examples

The getPrototypeOf() is an inbuilt function in JavaScript which is used to check the prototype of an object that the user has created. Most of the times, It is used to check whether two objects have the same prototype or not. Prototype here means the internal definition of the object that is defined by the user in the JavaScript code.

Syntax:

Object.getPrototypeOf(created_object);

Parameters: It accepts a parameter “created_object” which is the object of the entity whose prototype going to be found out.

Return Value: It returns the internal prototype of the object passed in the method.

JavaScript code to show the working of getPrototypeOf() function:

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
  // Creating a simple function
  function myfun(){}
    
  // creating a new object
  var obj= new myfun();
    
  // getting the prototype
  document.write(Object.getPrototypeOf(obj)); 
  
</script>

chevron_right


Output:

[object Object]

Code #2:
Another application of the getPrototypeOf() function is to check whether two objects have same prototype or not

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
   
  // Creating a simple function
  var first_var = function myFun() {};
    
  // Creating a object
  var second_var = Object.create(first_var);
  
  // Getting the output
  document.write(Object.getPrototypeOf(second_var === first_var));
  
</script>

chevron_right


Output:

false


My Personal Notes arrow_drop_up

A tech fanatic who can code stuff

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.