Why do we always get UNDEFINED on calling console.log ()?Whyyyyy?

By Arun Shukla

ยท

2 min read

Why do we always get UNDEFINED on calling console.log ()?Whyyyyy?

Hi Everyone, I am here again to share some interesting facts about your browser console. Why does every time it returns undefined even if you have asked something different to return? It's like the candy you get after paying your restaurant bill ๐Ÿ˜’๐Ÿ˜. Now let's understand why this happening so.

REPL

So for understanding this you have to first understand your browser compiler or terminal which is actually a REPL. Again what is REPL?

So, REPL stands for Read, Evaluate, Print, and Loop.

This is generally the coding environment tool that is a common feature among scripting languages like "node" for JavaScript, "python" for python, etc. It works in a systematic and straightforward way like PEMDAS. (Don't worry, It sounds weird๐Ÿ˜’ but it is actually like a BODMAS. Here P stands for Parenthesis and E stands for Exponent and the rest is the same as BODMAS.)

So, According to the full form of REPL, First, it will read the code you have written in your scripting language in the compiler. After that, it will evaluate all the operations on a code you have first written in your snippet. Then, it comes to the Print part which you want to return.

After all the stages the last part comes into action which is Loop. Here we are not talking about any for loop or while loop. Here the actual meaning of LOOP is that the computer is ready for taking more inputs after completing all three stages. So, in general terms after READ, EVALUATE, and PRINT operation computer prepares itself to take more inputs or run more operations. This is all about REPL.

Getting UNDEFINED

After understanding the REPL we are getting back to our topic. Suppose we want to print the message to our console as Hello HashNode.

console.log("Hello HashNode!...")

According to REPL, first, it will read the command then it will perform Evaluate Part. The evaluate part performs an operation like adding, subtracting or modifying an array and then returns something on the command like this:

. But Wait a Minute!! ๐Ÿ˜

There is nothing to evaluate in this command so, It will return an UNDEFINED.

just an example

Then it will go to the Print part where it will return the message you have passed in your console command. After that, it will move to the loop part where the compiler is again ready to take more instructions.

Conclusion:

So, If you understand the working of REPL then it will help you in understanding the behavior of your console command or any other instructions. If you have any doubts feel free to comment Guys...ThankYou..

ย