Objects can have methods
![Objects can have methods](/content/images/size/w1200/2022/07/object-methods-1.png)
In a previous lesson, we learned that an object can have properties.
![](https://www.minilessons.org/content/images/2022/07/real-world-objects--2-.png)
Similarly, objects can also have methods.
And methods are nothing but actions that can be performed on objects.
![](https://www.minilessons.org/content/images/2022/07/real-world-object-methods-2.png)
For example, if we take the car as an object, one of the actions we can perform on a car is starting its engine.
Similarly, one of the actions we can perform on a phone is calling someone.
One last example.
![](https://www.minilessons.org/content/images/2022/06/javascript-console--1-.png)
Let's imagine the Browser Console as an object.
One of the actions we can perform on the Console object is logging a message inside it for debugging purposes.
Actually, we don't have to imagine :D
The Browser Console is directly available to us as an in-built object in Javascript.
So, let's try to understand the concept of methods with the help of the Console object.
The Console object
We have been using the Console in the browser since the beginning of this course.
![](https://www.minilessons.org/content/images/2022/06/javascript-console--1-.png)
The good news is that Javascript's console
object will help us interact with the Browser Console through the code.
And the console
object comes packed with a lot of predefined methods.
So, we can use those methods to perform actions on the Browser Console.
Anyway, this is better shown than explained.
We can use the console
object in all three places where Javascript code can be executed:
- Inside the
<script>
tag of an HTML document. - Inside a file with
.js
extension. For example, themain.js
file. - Inside the Browser Console
This is fantastic because we can take our debugging to the next level.
For this lesson, let's use the Browser Console to try out the console
object.
So, open up the Browser Console and follow along with me.
Next, as I said before, one of the actions we can perform on the Console is logging a message inside it.
And for that, the console
object provides us with a log
method.
Now, let's see how to use the log
method.
So, how to use a method in Javascript?
A method is nothing but a property of an object.
In other words, the log
method is a property of the console
object.
So we can access the log
method using the Object dot notation:
console.log;
![](https://www.minilessons.org/content/images/2022/07/console-log-native-code.png)
And bang! What are we seeing?
Don't worry.
What we are seeing is the function definition of log
method.
A function definition simply contains a set of instructions for performing the action.
For example, the function definition of the log
method contains instructions for logging a message inside the Browser Console.
But why are we seeing a function definition?
Normally, when we try to access a property of an object inside the Console, we will see the data stored inside that property:
![](https://www.minilessons.org/content/images/2022/07/what-we-see-when-trying-to-access-object-property.png)
This tells us that the log
is not an ordinary property of the console
object.
Simply put, the log
method is storing a function
instead of a piece of data.
![](https://www.minilessons.org/content/images/2022/07/console-log-native-code.png)
Don't try to understand the above code. It is not necessary as of now.
I am gonna cut to the chase.
Here is another secret about methods in Javascript.
Methods are functions.
![](https://media.tenor.com/images/d7afa03456f5447f1899a1b0dfc3ab80/tenor.gif)
The log
method is nothing but a function like alert
, prompt
, etc.
So, we have to use the log
the method in the same way we use functions like alert
, prompt
, etc.
![](https://developer.mozilla.org/mdn-social-share.cd6c4a5a.png)
Simply put, we have to use the parentheses in order to execute the log
method and provide the message input in between the parentheses.
console.log("Hey developer! Cheers to objects and methods.");
![](https://www.minilessons.org/content/images/2022/07/methods-are-functions-1.png)
What we are seeing now is the result of the execution of instructions inside the log
method.
In simpler words, we are seeing our message logged inside the Console.
Anyway, let's quickly perform another action on the Browser Console.
I want to clear the existing code inside the Console using the console.clear()
method.
Actually, the console
object is more useful than what we just saw.
In the next lesson, we will see what other actions we can perform on the Console.