I have the following function
const prueba= () =>{
let equis = console.log("Hola")
}
console.log(prueba());
The function returns "Hello undefined" I understand that the undefined is because I am calling the function prueba
and not return anything explicit, but the Hola
? I don't understand why I'm returning something from a variable that I'm not even calling or using.
You get undefined because you are not returning any value from the function and you are printing the result.
You have 2 options.
This happens because by not returning a value from a function, javascript returns undefined by default, that is, your function is the same as putting:
Remember that in arrow functions if the body of the function is just an expression, you can omit the brackets and the return:
Answering your question, it prints Hello because you have it inside a console.log(), and it prints undefined because you have the function call inside another console.log();
First, the function returns nothing (undefined) because there is no
return
.Second,
console.log
it writes something to the console, it doesn't return anything, so itlet equis = console.log("Hola")
doesn't make any sense (equis
it will do tooundefined
).To finish, the line
console.log(prueba());
will first execute the function, that's why it first shows "hola
" in the console, and then it will show what the function returns, which is nothing, that is, the "undefined
", that's why it greets the console with "hola undefined
", but it returns nothing.Maybe what you wanted looks like this: