DONATE  
MENU
HTML
JAVASCRIPT
AJAX
JSON

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Functions » The Return Keyword
Share Tweet WhatsApp

The Return Keyword

The return keyword as used inside a function is used in returning a value of any data type from a function. When such a function returning a value is called on a variable, the returned value is thereafter stored on the variable for further operations you may intend.
<script>
function myFunc(){
return 67; //returns 67 as a value
}

var x = myFunc(); //67 will be stored on variable "x"

alert(x);
</script>

Copy PRACTICE IT

The value returned by a function can also be use directly in a statement or expression without needing to store it on a variable first.
<script>
function num(){
return 3;
}

var c = 2 * num(); //same as 2 * 3 (which was returned from the function)

alert(c);
</script>

Copy PRACTICE IT

Returning a value from a function is not same thing as outputting a value in a function. You may think when you alert a text or output texts in a function and call the function on a variable, the alerted text will be stored on the variable but NO... it will output your text while the variable remains undefined.
<script>

function helloWorld(){
alert("Hello World");
}

var hello = helloWorld(); //undefined
alert(hello);
</script>

Copy PRACTICE IT

The return keyword is the only way to return a value that you can use from a function. NOTE
  • Once the return keyword is used in a function, that marks the end of the function as no other statements will be executed.
    <script>
    function myFunc(){
    alert("BEFORE RETURN KEYWORD");
    
    return 1;
    
    alert("AFTER RETURN KEYWORD"); //Will not execute
    }
    
    var num = myFunc();
    
    </script>

    Copy PRACTICE IT

  • Therefore, be sure to have finish your operations before returning a value.
You can return any value of whatever data type(number, string, boolean, array, object) from a function.
<script>
function addNum(num1, num2){
var sum = num1 + num2; //adding the numbers

return sum; //returning the sum
}

var ans = addNum(8, 15); //storing what was returned

alert(ans) //outputting it

</script>

Copy PRACTICE IT

SUMMARY
  1. The return keyword enables us to return a value of whatever data type from a function.
  2. Whenever such functions are called, the value is returned and can therefore be stored or use directly in whatever operations.
  3. Outputting text or value in whatever way in a function is different from returning a value.
  4. Once a value is returned in a function, the function ends and no other statements in it are executed.
Take a short quiz on The Return Keyword.
Ask a question or tell us if The Return Keyword is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode