FOLASAYO SAMUEL OLAYEMI
SV_D_CURIOUS_SOFTWARE_ENGINEER'S BLOG

SV_D_CURIOUS_SOFTWARE_ENGINEER'S BLOG

The difference between a static method and an instance method

Photo by Andrew Neel on Unsplash

The difference between a static method and an instance method

FOLASAYO SAMUEL OLAYEMI's photo
FOLASAYO SAMUEL OLAYEMI
·May 4, 2022·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Static methods belong to a class and don't act on instances, while instance methods belong to the class prototype, which is inherited by all instances of the class and acts on them.

Code Sample:

Array.isArray // static method of Array
Array.prototype.push // instance method of Array

In this case, the Array.isArray method does not make sense as an instance method of arrays because we already know the value is an array when working with it.

Instance methods could technically work as static methods, but provide terser syntax:

Code Sample:

const arr = [1, 2, 3]
arr.push(4)
Array.push(arr, 4)

Thanks for reading...

Happy Coding!

 
Share this