- Define a JavaScript method
In the previous lessons, we did a very important thing in the JavaScript language, we called a method. We glossed over some of the detail about what was happening at the technical level at that time. Let's put some new vocabulary around what we did.
Let's consider the document.querySelector
method.
This code calls the method querySelector
. The
method is said to be "defined in" the object document
.
Calling is the same as running. We do that when we write:
document.querySelector(); // Notice the addition of ()
When we call document.querySelector()
, we provide it an argument, a
bit of text, a String
of text that goes inside the ()
.
The method document.querySelector()
expects us to provide a CSS
identifier that will help us find the node we want as an argument.
If the document
object finds an HTMLElement
in its representation of the page, it returns
it. Otherwise the method returns null
.
The thing document.querySelector()
returns is also an object. It, too, has
both information and methods, state and behavior, properties and methods (they
all mean the same thing). This HTMLElement
instance has methods like remove()
.
JavaScript methods are actions that can be performed on objects. Objects have
many useful methods that help us modify and iterate through them, for example.
The DOM selection method document.querySelector()
is a powerful tool for finding
elements needed to make updates and changes to the DOM using methods like remove()
.