@@ -273,23 +273,38 @@ <h3 id="functions">Functions</h3>
273273 </ p >
274274 < p > We define a function like this:</ p >
275275 < pre > < code class ="language-javascript "> < span > < span > function</ span > < span > sayHello</ span > (< span > </ span > ) </ span > {
276- // add function code here...
277276 < span > console</ span > .log(< span > "Hello!"</ span > );
278277}
279278</ code > </ pre >
280- < p > and call (execute) it like this:</ p >
279+ < p >
280+ In this case, the function < code > sayHello</ code > only has one line of code
281+ (the console.log statement). Anything between the the < code > {</ code > and
282+ < code > }</ code > is the function code.
283+ </ p >
284+ < p >
285+ Now we can call (execute) the function by typing out its name and adding
286+ < code > ()</ code > :
287+ </ p >
281288 < pre > < code class ="language-javascript "> sayHello()< span > ;</ span >
282289</ code > </ pre >
283290 < p >
284- Notice the < code > ()</ code > at the end - they are a clear sign that
285- < em > sayHello</ em > is a function. Without the < code > ()</ code > the function
286- will not be executed.
291+ The < code > ()</ code > at the end are a clear sign that < em > sayHello</ em > is a
292+ function. Without the < code > ()</ code > the function will not be executed.
293+ </ p >
294+ < p >
295+ So now, wherever we type < code > sayHello();</ code > , the code of our function
296+ (in this case the output "Hello!") will be executed.
297+ </ p >
298+ < p >
299+ (Btw, you can also call the function inside of itself - but that will create
300+ an infinite loop and crash your browser. So, uh, don't do that.)
287301 </ p >
288302 < br />
289303 < h4 > Parameters</ h4 >
290304 < p >
291305 Functions can also take so-called parameters. Those are just variables that
292- you fill in when you call a function.
306+ you fill in when you call a function. This way you can make your function do
307+ different things depending on which parameters you use while executing it.
293308 </ p >
294309 < p >
295310 For example, this function takes two parameters, which can be used as
@@ -298,17 +313,27 @@ <h4>Parameters</h4>
298313 < pre > < code class ="language-javascript "> < span > function</ span > < span > introduceMyself</ span > (name, age) {
299314 console.log("Hello! My name < span > is</ span > < span > " + name + "</ span > < span > and</ span > I am < span > " + age + "</ span > years old.< span > ");
300315}</ span >
316+ < p > Now you can use this function to output the introduction, no matter which name and age is given.</ p >
301317</ code > </ pre >
302318 < p > You'd call it like this:</ p >
303319 < pre > < code class ="language-javascript "> < span > introduceMyself</ span > (< span > "Petra"</ span > , < span > 26</ span > );
304320</ code > </ pre >
321+
305322 < p > or like this, using our variables from before:</ p >
306323 < pre > < code class ="language-javascript "> introduceMyself(< span > myName</ span > , myAge)< span > ;</ span >
307324</ code > </ pre >
308325 < p > or any combination:</ p >
309326 < pre > < code class ="language-javascript "> introduceMyself(< span > "Petra"</ span > , myAge)< span > ;</ span >
310327</ code > </ pre >
311- < p > You can see here that the order of variables is important.</ p >
328+ < p >
329+ You can see here that the order of variables is important, because the
330+ values you use while executing the function should match the order of
331+ variable names you defined when creating the function. (In this case,
332+ "Petra" becomes the value of the variable < code > name</ code > because it is
333+ the first parameter, and 26 will be the value of < code > age</ code > because it
334+ is the second parameter.)
335+ </ p >
336+
312337 < br />
313338 < h4 > Return</ h4 >
314339 < p >
@@ -320,18 +345,15 @@ <h4>Return</h4>
320345 < span > return</ span > number1 + number2;
321346}
322347</ code > </ pre >
323- < p >
324- Here is an example using a string as a return value (also notice how I use
325- < code > +</ code > to combine strings):
326- </ p >
348+ < p > Here is an example using a string as a return value:</ p >
327349 < pre > < code class ="language-javascript "> < span > < span > function</ span > < span > getIntroduction</ span > (< span > name, age</ span > ) </ span > {
328350 < span > let</ span > introduction = < span > "Hello! My name is "</ span > + name + < span > " and I am "</ span > + age + < span > " years old."</ span > ;
329351 < span > return</ span > introduction;
330352}
331353</ code > </ pre >
332354 < p >
333355 When calling a function with a return value you could then save its return
334- value in a newly created variable like so :
356+ value in a newly created variable and then use that variable :
335357 </ p >
336358 < pre > < code class ="language-javascript "> < span > let</ span > myIntroduction = getIntroduction(myName, myAge);
337359console.< span > log</ span > (myIntroduction);
@@ -341,6 +363,44 @@ <h4>Return</h4>
341363 </ p >
342364 < pre > < code class ="language-javascript "> console.< span > log</ span > (getIntroduction(myName, myAge));
343365</ code > </ pre >
366+
367+ < br />
368+ < h4 > Existing functions</ h4 >
369+ < p >
370+ By now you might have noticed that the < code > console.log();</ code > statement
371+ we use to output text in the console is also a function! To be exact,
372+ < code > log</ code > is a function of the object < code > console</ code > . (We know
373+ that < code > log</ code > is a function and not a variable because it has the
374+ < code > ()</ code > at the end and it can take parameters - in this case the
375+ text to output.)
376+ </ p >
377+ < p >
378+ < code > console.log()</ code > is just one of many pre-existing functions in
379+ JavaScript that are useful. Here are some examples of functions you can use
380+ to modify text. The syntax for these is
381+ < code > variableName.functionName()</ code > :
382+ </ p >
383+ < pre > < code class ="language-javascript "> let myTextVariable = "Text";
384+ myTextVariable.toUpperCase(); // turns "Text" into "TEXT"
385+ myTextVariable.toLowerCase(); // turns "Text" into "text"
386+ myTextVariable.split(); // turns "Text" into ["T", "e", "x", "t"]
387+ myTextVariable.includes("Tex"); // return true because "Text" includes "Tex"
388+ myTextVariable.startsWith("T"); // return true because "Text" starts with "T"</ code > </ pre >
389+ < p >
390+ Keep in mind that all of these are case sensitive. This means it's
391+ < code > toUpperCase()</ code > < b > not</ b > < code > toUppercase()</ code > . This also
392+ means that < code > myTextVariable.startsWith("t")</ code > would return false
393+ because "Text" does not include a lowercase t.
394+ </ p >
395+ < p >
396+ Btw, to use the results of these function you'd need to save the value into
397+ a variable, because the functions do not modify your variable value. That
398+ means: After calling < code > myTextVariable.toUpperCase()</ code > ,
399+ < code > myTextVariable</ code > is still "Text", < b > not</ b > "TEXT". To save it
400+ we could do something like this:
401+ </ p >
402+ < pre > < code class ="language-javascript "> let myTextVariable = "Text";
403+ let myTextVariableUppercase = myTextVariable.toUpperCase();</ code > </ pre >
344404</ section >
345405
346406<!-- --------------------------------------------------------------- -->
0 commit comments