0% found this document useful (0 votes)
8 views6 pages

JavaScript Static Methods

The document explains JavaScript static methods, which are defined on the class itself and cannot be called on an object instance. It provides examples demonstrating how to use static methods and how to pass an object as a parameter to a static method. The document is part of a tutorial on JavaScript classes and methods.

Uploaded by

samerguda13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

JavaScript Static Methods

The document explains JavaScript static methods, which are defined on the class itself and cannot be called on an object instance. It provides examples demonstrating how to use static methods and how to pass an object as a parameter to a static method. The document is part of a tutorial on JavaScript classes and methods.

Uploaded by

samerguda13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

 Tutorials  Exercises  Services   Get Certified Sign U

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS

JavaScript Static Methods


‹ Previous Next ›

Static class methods are defined on the class itself.

You cannot call a static method on an object, only on an object class.

Example

class Car {
constructor(name) {
this.name = name;
}
static hello() {
return "Hello!!";
}
}

const myCar = new Car("Ford");

// You can call 'hello()' on the Car Class:

https://www.w3schools.com/js/js_class_static.asp 02/11/2024, 10 51 AM
Page 1 of 6
:
document.getElementById("demo").innerHTML = Car.hello();

// But NOT on a Car Object:


// document.getElementById("demo").innerHTML = myCar.hello();
// this will raise an error.

Try it Yourself »

If you want to use the myCar object inside the static method, you can
send it as a parameter:

Example

class Car {
constructor(name) {
this.name = name;
}
static hello(x) {
return "Hello " + x.name;
}
}
const myCar = new Car("Ford");
document.getElementById("demo").innerHTML = Car.hello(myCar);

Try it Yourself »

‹ Previous Next ›

W3schools Pathfinder

Sign Up Log in

https://www.w3schools.com/js/js_class_static.asp 02/11/2024, 10 51 AM
Page 2 of 6
:
Sign Up Log in
Track your progress - it's free!

ADVERTISEMENT

COLOR PICKER



ADVERTISEMENT

https://www.w3schools.com/js/js_class_static.asp 02/11/2024, 10 51 AM
Page 3 of 6
:
 PLUS SPACES GET CERTIFIED

FOR TEACHERS FOR BUSINESS

CONTACT US

Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

https://www.w3schools.com/js/js_class_static.asp 02/11/2024, 10 51 AM
Page 4 of 6
:
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be
simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid
errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and
accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools


is Powered by W3.CSS.

https://www.w3schools.com/js/js_class_static.asp 02/11/2024, 10 51 AM
Page 5 of 6
:
https://www.w3schools.com/js/js_class_static.asp 02/11/2024, 10 51 AM
Page 6 of 6
:

You might also like