1️⃣

6.1 What Are Methods?

6.1 What Are Methods?

Methods are a set of statements that take inputs, perform some computations, and produce some output.
The main usage of methods is to put commonly used code into one method, and call that method whenever you need to use the commonly used code.
This reduces the amount of code found in your file and just makes your life a lot easier. This is especially the case if you are working with others.

Example

function logMe(name) { console.log(name); } logMe("Matthew"); logMe("Matthew"); logMe("Matthew"); // output: // Matthew // Matthew // Matthew

Method Creation

Here’s the typical method structure:
function functionName(Parameter1, Parameter2, ...) { // Function body }
First thing we have to do is use the function keyword.
Then, we need to give it a name. Typically it follows the camel case structure.
💡
Camel case is where the first word in the name is lowercase and any other word is capitalized.
// ex. logMe performSearch findMeTheBestPizza
Then, we give it parameters which are really just placeholder names for the inputs. You can put as many parameters as you want.
Finally, you have your function body. This is where you define what the method does.

Here’s a method I made:
function logMe(name) { console.log(name); } logMe("Matthew"); logMe("Matthew"); logMe("Matthew"); // output: // Matthew // Matthew // Matthew
Here the method name is logMe. It has a parameter called name. The parameter is used in the method body.
If you want return something from the function, you can do something like the following:
function createSentence(name) { return "No one likes " + name + "."; } console.log(createSentence("Matthew"));
Here the return keyword tells the function to stop and give back a string. Return is used a lot in programming, so get used to it!

Next Section

2️⃣
6.2 Arrow Method
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.