Methods complex like a puzzle - pictured as word Methods on a puzzle pieces to show that Methods can be difficult and needs cooperating pieces that fit together, 3d illustration

Methods

In JavaScript, methods are function that exist on an object. Here is an example…

class Animal {
    constructor(name){
        this.name = name;
    }
    grunt(){
        return ${this.name} says “ugh!”;
    } }
let gorilla = new Animal(‘Bongo’);
console.log(gorilla.grunt()); 

This code would output Bongo says “ugh!”. Notice that we did not use the function keyword. We did need the return though.

Note: You might notice that we used backticks in this example. You also might notice something odd. We used a dollar sign ($) and curly braces ({}) in our line of log code. This syntax allows for interpolation. We can put a variable inside the curly braces and the code will display the value of the variable.

Happy Coding!

Clay Hess

Share this post

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email
Skip to content