0
var a = "happy";
// -> undefined

This is code from the console. Why when we declare anything like var a = "happy" in the console after clicking the enter the next line , why it showing undefined?

What is happing in the console?

1
  • 2
    The console is outputting the response from the command you entered in to it. Declaring a variable has no response, so you see undefined. Commented May 8, 2023 at 15:12

1 Answer 1

1

That is because the operation doesn't return anything. It simply stores a value in a variable and doesn't compute a result to display in the console. This is very similar to a call to a function with an empty return statement or no return statement at all. In order to access the stored value, you will need a second line simply saying a;. Beside, the use of var is now deprecated and I would advise using let instead.

Sign up to request clarification or add additional context in comments.

1 Comment

This is an often-repeated duplicate question. Please don't post answers to duplicates. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.