Methods are functions that are associated with an object. They can be accessed and invoked using the dot notation. Methods are useful for encapsulating behavior that is specific to an object and can be called upon whenever needed.
To create a method, we define a function as a property of an object. This function can then be called through the object using the dot notation.
Once a method is defined, we can invoke it using the object's name followed by the dot operator and the method name, followed by parentheses.
person object, add a method called
sayAge that logs the age of the person.
sayAge method to check if it logs the correct age.
Sign up to start coding
Already have an account?