JavaScript Functions: Why They're Essential to Understand? | Easy Guide
Tags: webdev, javascript, beginners, tutorial
Published: 2022-12-06 13:32:36

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading -->
<h2><a href="https://nerdleveltech.com/how-to-learn-programming-all-you-need-to-know-guide/" target="_blank" rel="noreferrer noopener">Introduction</a>: What are JavaScript functions?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>A JavaScript <a href="https://nerdleveltech.com/javascript-foreach-is-not-a-function-and-what-to-do/" target="_blank" rel="noreferrer noopener">function</a> is a block of code which <a href="https://nerdleveltech.com/how-to-be-better-at-algorithms-examples-for-beginners/" target="_blank" rel="noreferrer noopener">performs a specific task</a>. They are used to break down and organize the code into manageable chunks. Functions can be used to create custom objects, filters, and many other things.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript" target="_blank" rel="noreferrer noopener">JavaScript</a> functions are defined using the function keyword followed by a name for the function and a pair of parentheses containing zero or more comma-separated parameters (or arguments). The function parameter list will be in between those parenthesis ( ). The parameters are optional and we might call them variables inside the a functions. The body of the function is enclosed in curly braces {}, which may include one or more statements inside them.</p>
<!-- /wp:paragraph -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p><strong>What is an example of a <a href="https://www.w3schools.com/js/js_functions.asp" target="_blank" rel="noreferrer noopener">function in JavaScript</a>?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>function Name (parameter1, parameter2) {</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>  statements;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>  return value;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>}</p>
<!-- /wp:paragraph -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3>Lets break it down to:</h3>
<!-- /wp:heading -->

<!-- wp:list {"fontSize":"medium"} -->
<ul class="has-medium-font-size"><!-- wp:list-item -->
<li>The function keyword</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>The name after the keyword</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Optional parameters</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Statements that carries the tasks</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Return keyword to get some output</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p>Now let’s create a real example, let us start with a function that can add two numbers:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>function add(num1, num2) {

  let result = num1 + num2

  console.log(result)

return result

}</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>This function can add two values, but first we need to call the function with the two values that we want them added (Argument1, Argument 2):</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>add(2, 3)

// output: the return statement should return : 5</code></pre>
<!-- /wp:code -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p>Another good thing from JavaScript is that it let you keep the output of a function into a variable, so instead of the add(2,3) we can write:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>const addOutput = add(2,3)

// output: the return statement should return the addOutput's variable with : 5</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>With that I stored the value of the function into a variable and I don’t have every single time to call the function with same arguments passed on to get the same output. Instead only use the addOutput variable that will store the add function’s returning value.</p>
<!-- /wp:paragraph -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p><strong>Things to keep in mind:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"fontSize":"medium"} -->
<ul class="has-medium-font-size"><!-- wp:list-item -->
<li>The variables names are case sensitive. addoutput is different than addOutput, we use the camel case in javaScript. Start with lower char than with each other word we go upper for the first char only.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Using descriptive names, avoid x, y or z names for the parameters and functions names.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Writing the function called declaring the function</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Calling the function called invoking or calling the function</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:image {"align":"center","id":829,"width":512,"height":384,"sizeSlug":"large","linkDestination":"custom"} -->
<figure class="wp-block-image aligncenter size-large is-resized"><a href="https://nerdleveltech.com/best-web-development-books-you-need-to-learn/" target="_blank" rel="noopener"><img src="https://nerdleveltech.com/wp-content/uploads/javascript-2022-10-26-06-14-20-utc-1-1024x768.webp" alt="JavaScript Functions Tutorial" class="wp-image-829" width="512" height="384"/></a><figcaption class="wp-element-caption">JavaScript Functions Tutorial</figcaption></figure>
<!-- /wp:image -->

<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading -->
<h2>Declarations</h2>
<!-- /wp:heading -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3>Declare a function that called an expression</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Just as the way we used the declare a variable to store the values returned from a function above, we can use the same technique to declare a function, and it is called the function expression</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is how:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>const add = function(num1, num2) {

  let result = num1 + num2

  console.log(result)

  return result

}


add(2,3)

// output: 5</code></pre>
<!-- /wp:code -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3>Function that called IIFE (Immediately invoked function expression)</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>An IIFE is a type of a function that we need to write when we want the function to run, called or invoked immediately.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>(function (num1, num2) {

let result = num1 + num2

console.log(result)

})(2,3)

// output: 5
// Notice we didn't have to call the function by a name, also notice the function has no name!</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Things to keep in mind with <strong>function</strong></strong> <strong>expression:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"fontSize":"medium"} -->
<ul class="has-medium-font-size"><!-- wp:list-item -->
<li>Expressions are anonymous, so we'll see later in this post why it's important that we should be familiar with the different kinds of functions.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>IIFE is an expression type of functions as well</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3><strong>Parameters in javascript:</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We only call a parameter an argument when it carries a value. By default, JavaScript's has an array-like object Arguments to you access all the parameters passed to a function without having to name or use them one by one.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>function add() {

let argsTotal = 0

for (let i = 0; i &lt; arguments.length; i++) {

// Here we see that javascript give us an access to the object like array called arguments

argsTotal += arguments&#91;i]

}

return argsTotal

}

const output = add(2,3,1)

console.log(output) // 6</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>With the recent version of JavaScript, we got another present called the rest operator. This is basically give us what arguments (array-like object) does, but in a better way. And by better way, I mean that now I have an array that I can use all <a href="https://nerdleveltech.com/a-guide-to-convert-strings-to-arrays-and-vice-versa-in-javascript/" target="_blank" rel="noreferrer noopener">array's methods</a> with, like sort(), filter(), map(), or reduce().</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>function add (...args) {

let argsTotal = args.reduce(function(sum, value) {

return (sum + value)

})

return argsTotal

}

const output = add(2,3,1)

console.log(output)
// output: 6</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong>Things to keep in mind about parameters:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"fontSize":"medium"} -->
<ul class="has-medium-font-size"><!-- wp:list-item -->
<li>This array like object does not have an access to some array methods like map, filter or sort</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>More recent version of javascript, let you use the rest operator with as the last parameters within the function: function add(num1, num2, …args) {} - or just for simplicity: function add(…args) {}</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>This rest parameters can accept an infinite numbers of arguments, and its always should be at the end of the parameters list</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>While the Arguments object isn’t a real array, the rest parameters is and you could use all the array's methods with it</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>When I pass an object as an argument? when I need to pass a value by reference and I don’t care about the order of the arguments</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:image {"align":"center","id":831,"width":480,"height":384,"sizeSlug":"full","linkDestination":"custom","className":"is-style-default"} -->
<figure class="wp-block-image aligncenter size-full is-resized is-style-default"><a href="https://nerdleveltech.com/best-web-development-books-you-need-to-learn/" target="_blank" rel="noopener"><img src="https://nerdleveltech.com/wp-content/uploads/webstacks-ewXKiIezeTg-unsplash.webp" alt="JavaScript Functions Tutorial" class="wp-image-831" width="480" height="384"/></a><figcaption class="wp-element-caption">JavaScript Functions Tutorial</figcaption></figure>
<!-- /wp:image -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3><strong>Scope</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In order to understand the scope, we need to take a look at a variable. You need to know where it is declared?, by what it is declared?. By where I mean on which block {} or which line. By what i mean by what keyword (var, let or const keywords) or if it's normal declaration or expression. Let’s just say never use "var" keyword :)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Although I asked not to use it, but you will definitely run into a code that uses it. So, you should be familiar with it to avoid any troubles. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>var nerdLevel = "nerd"

function doStudy() {

nerdLevel = "not nerd"

console.log(nerdLevel)

}

doStudy()
//output: not nerd</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>In the previous example nerdLevel variable defined in the global scope, notice that we didn’t declare it inside the function, yet we can use it and change it’s value.</p>
<!-- /wp:paragraph -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p>In the following example we use the nerdLevel variable in function scope using "var":</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>
function doStudyNerd() {

var nerdLevel = "Not Nerd"

console.log(nerdLevel)

}

doStudyNerd()

//output Not Nerd</code></pre>
<!-- /wp:code -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p>Let’s take a look now on the only block scope keywords variable declarations: let and const. In the following example we can change let to const and we will get the same result. However, we only need to use const if we know that its value will not change. Hence the name const from constant. Or we can use it for the passed references object like arrays and functions.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>let nerdLevel= "nerd"

function doStudy() {

let nerdLevel = "not nerd"

console.log(nerdLevel)

}

doStudy() // output: not nerd

console.log(nerdLevel) // output: nerd</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Those two variables with the same name should not be allowed, but if you notice the same name yes, but it shows in different occasions. Hence, the scope is different and it's not the same. The first time it appears, it was within the global scope, so when you call it in the global scope, it shows the value of “nerd”.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The other occasion was when you created it inside the function, which is why when you invoked the function, it only showed its value because it lives within that function.</p>
<!-- /wp:paragraph -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p><strong>Things to keep in mind about scope:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"fontSize":"medium"} -->
<ul class="has-medium-font-size"><!-- wp:list-item -->
<li>If you didn’t use a declaration keyword like let, var or const javascript will assume that you want to use var</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3>Hoisting</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Hoisting is the process of moving a variable declaration to the top of its containing scope, in order to make it available as soon as possible.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you have a function declared on line number 300 but you actually invoked it or call this very same function in line number 100.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Logically this is impossible to execute, but luckily with javascript the compiler actually can hoisted before the invoking or calling so it dost not results an error. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Remember that I told you above that it's important that we know which type of function we should pick before the declarations? normal or expressions. Because the hoisting happens when you declare the function in the normal way, not the expression way when you anonymously store it inside a variable.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Things to keep in mind about hoisting:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"fontSize":"medium"} -->
<ul class="has-medium-font-size"><!-- wp:list-item -->
<li>Keyword const is the same as let keyword, but const used if we want to use a constant value or use a declaration expression.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Function's normal declarations are like var· They will be hoisted, whereas function expressions won't.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3><strong>Methods</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Methods are one of the procedures and actions that can be performed on an object in JavaScript. In the following examples, we will be looking at how to create and invoke a method within an object.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>The keyword this</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Whenever we use the keyword this in JavaScript code, we mean an object. That's why it's always a good idea to see the context in which you're using the keyword this, as the answer always depends on that.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>const nerdsInfo = {

social: {

blog: "nerdleveltech.com",

twitter: "twitter.com/NerdLevelTech",

reddit: "www.reddit.com/user/NerdLevelTech"

},

printSocial: function(data) {

console.log(`${data.blog} ${data.twitter} ${data.reddit}`)

}

}

nerdsInfo.printSocial(nerdsInfo.social)

// output: nerdleveltech.com twitter.com/NerdLevelTech www.reddit.com/user/NerdLevelTech</code></pre>
<!-- /wp:code -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p><strong>But with using keyword this we can do that instead:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>const nerdsInfo = {

social: {

blog: "nerdleveltech.com",

twitter: "twitter.com/NerdLevelTech",

reddit: "www.reddit.com/user/NerdLevelTech"

},

printSocial: function(data) {

console.log(`${this.social.blog} ${this.social.twitter} ${this.social.reddit}`)

}

}

nerdsInfo.printSocial()

// output: nerdleveltech.com twitter.com/NerdLevelTech www.reddit.com/user/NerdLevelTech
</code></pre>
<!-- /wp:code -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p><strong>And if we wan't to enhance the code a little bit and make it more practical example, we can write the following:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>const nerdsInfo = {

social: {

blog: "nerdleveltech.com",

twitter: "twitter.com/NerdLevelTech",

reddit: "www.reddit.com/user/NerdLevelTech"

},

printSocial: function() {

for (const key in this.social){

console.log(`${key} ${this.social&#91;key]}`)

}

}

}

nerdsInfo.printSocial()

// output:

// blog nerdleveltech.com

// twitter twitter.com/NerdLevelTech

// reddit www.reddit.com/user/NerdLevelTech
</code></pre>
<!-- /wp:code -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p><strong>Things to keep in mind about keyword this:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"fontSize":"medium"} -->
<ul class="has-medium-font-size"><!-- wp:list-item -->
<li>If we use the keyword this within a function scope, we mean the global object at that time</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>If we use the keyword this within object scope, we mean that object at that time</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:image {"align":"center","id":841,"width":512,"height":341,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-large is-resized"><img src="https://nerdleveltech.com/wp-content/uploads/risk-return-concept-2022-10-24-22-43-29-utc-1024x682.webp" alt="Return Statement" class="wp-image-841" width="512" height="341"/><figcaption class="wp-element-caption">Return Statement</figcaption></figure>
<!-- /wp:image -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":3} -->
<h3><strong>The return statement</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The keyword return represents the results of a function after we return the results, the execution of this function is done. In other words it ends the function's execution</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>function add (num1, num2) {

return console.log('Add Results: '),

num1 + num2

}

console.log(add(2,3))

// output: Add Results: 5</code></pre>
<!-- /wp:code -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p>What if we take a new line after the return? what would you think the results of the code would be?</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>function add (num1, num2) {

return

console.log('Add Results: '),

num1 + num2

}

console.log(add(2,3))

// output: undefined</code></pre>
<!-- /wp:code -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p>Can you make an educated guess? and why?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The reason for undefined result is the compiler automatically put at the end of each line the semicolon “;“</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So when the compiler assume that this is the end of the line, it will be only return; and that of course return undefined.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now you need to include the parenthesis like the following:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>function add (num1, num2) {

return(

console.log('Add Results: '),

num1 + num2

)

}

console.log(add(2,3))
// output: Add Results: 5</code></pre>
<!-- /wp:code -->

<!-- wp:spacer {"height":"20px"} -->
<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:paragraph -->
<p><strong>Things to keep in mind about return statement:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"fontSize":"medium"} -->
<ul class="has-medium-font-size"><!-- wp:list-item -->
<li>Multiple returns can be in the same function with a conditional statements, once any of the return be true, the function will return at that point and the function will end</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>If there is no return the function when invoked, the function will return undefined</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->