Table of contents
- -Everything after the return statement doesn't run
- -You can return anything in a javascript function using the return statement
- -You can use the return statement multiple times under certain conditions
- -You can assign the value from your return statement to a variable
- -If a return statement is written on multiple lines then you might need to use parentheses
Hello Hashnode family💜,
Welcome back to my blog😊. So a friend of mine who recently just started learning javascript reached out to me and said, "Return in javascript is giving me a tough time." I quickly scrambled around to get him articles on return statements in javascript and sent him links. They say every writer has their own unique voice and there's someone somewhere that can specifically relate to that voice, so I decided to write on the topic today, perhaps maybe there's someone else in my friend's position that might connect more to my writing.
The return statement in javascript is simple yet logical and essential⭐. It's also very very very common in javascript. You'll find the return statement in several javascript functions, you've also probably used it several times. There so many things to note about the return statement and I'll be discussing a few of them below.
-Everything after the return statement doesn't run
You'd have noticed that the return statement is mostly the last statement written in a javascript function. This is mostly because it is assumed to give the conclusion of what the function is supposed to do or what value it's supposed to get finally. So any line of code after it in a function is neither read nor executed.
function returnStatement () {
return "I am the returned value"
console.log('I am after the return statement')
}
In the example above, " console.log('I am after the return statement')" will not be read or executed but that will change if it is placed above the return statement
-You can return anything in a javascript function using the return statement
When you write a javascript function, you can simply just return a number, a string, a boolean, an object, an array... In fact, you can return another function. If you run a javascript function without returning anything from it using the return statement. Javascript automatically returns an undefined value from the function.
-You can use the return statement multiple times under certain conditions
The return statement can be used multiple times if each is placed in its own code block
function returnStatement (a) {
if (a < 5) {
return "I am a small number"
}
else {
return "I am a large number"
}
}
-You can assign the value from your return statement to a variable
You can simply declare a variable and equate it to your function in action and this will help assign the value being returned by the function to that variable.
function returnStatement () {
return "I am a small number"
}
let result = returnStatement(); //I am a small number
-If a return statement is written on multiple lines then you might need to use parentheses
If parentheses aren't used, you might get an undefined as your returned value.
I guess you now have a fair understanding of return statements in javascript. Kindly drop any additional information in the comment section. See you in my next post😘