During learning JS most of the time I tend to open Google Chrome and open the console tab to test the codes. But whenever I typed my code, the output will show in the console(not all the time), also there is an output that always sticks in that console which is "undefined".
To test this undefined problem in the console tab, I opened some other browsers like Brave and Microsoft Edge. There was also the same problem I faced. After scraping in google I found out, that most browser shows this undefined when there is no return happening in the function.
function newApp(name){
const userName = "Hii " + name;
console.log(userName);
return userName;
}
newApp("Abhisek");
Output is the same as you expected:
You can clearly see that this function is return some value in this case is userName
. But when you didn't return any values, then this "undefined" knocks on your door.
function newApp2(name){
const userName = "Hii " + name;
console.log(userName);
}
newApp2("Abhisek");
Output is the same as you expected:
Not only chrome shows this undefined
but also Brave and Microsoft Edge.
Microsoft Edge:
Brave Brower:
###