CLASS web

JavaScript

return

return

return is a command that returns a value.

Executing statements inside a function and returning exits the function. And return what's in the return statement

For example,

function hey(a,b){
    add = a+b;
    return add;
}

If you write like above

The hey function returns the value of the add variable.
Of course, to see the returned value, you must call the function in the output text.

document.write(hey(1,2));

If you print as above, add is 3 in the return statement.

Source

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JavaScript</title>
<script>
    function hey(a,b){
        add = a+b;
        return add;
    }
    document.write(hey(1,2));
</script>
</head>
<body>
</body>
</html>

I'll check the results right below.







So in the source above, let's put 20 instead of add in the return statement. Doing so will return 20.

Source

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JavaScript</title>
<script>
    function hey(a,b){
        add = a+b;
        return 20;
    }

    document.write(hey(1,2));
</script>
</head>
<body>
</body>
</html>

I'll check the results right below.







end





ALL COMMENTS 0

Sort by

PinkCoding

PinkCoding

X

PinkCoding

Web Scratch Pad

X

loading

            
            
        

Back to the course