Console.log() and Undefined?

Joined
Mar 29, 2023
Messages
26
Reaction score
1
I have been trying to learn JavaScript and everything has been alright so far. Then i tried to output a value for a variable i had created
using console.log() and at each and every attempt the output has been the same "undefined". The tutorials I have come across shows the following:

Code:
                     console.log("Hello, World!");
                     Output: undefined

is there anyway i can get around this in Firefox?
 
Joined
Aug 22, 2023
Messages
66
Reaction score
18
Did you nest the console.log() function in a script tag (this only applies if your code was a .html file)? If it was written in Javascript (.js), this does not apply. I will provide two examples of how to log a message to the console, one in a .js file and one in a .html file.
HTML:
<!DOCTYPE html>
<html>
    <head>
        <title>HTML Console</title>
    </head>
<body>
    <p>How to log to the console in a .html file.</p>
    <script>
        console.log("Hello World");
    </script>
    </body>
</html>
JavaScript:
console.log("Hello World");
Hopefully you can see the distinction between the two filetypes and what is needed to log something to the console in these respective filetypes.
 
Joined
Mar 29, 2023
Messages
26
Reaction score
1
Hi Saul H
I can see and understand where you are coming from with the first example but would like to see
the second .js example if you don't mind. Thanks
 
Joined
Aug 22, 2023
Messages
66
Reaction score
18
I provided the example. It's purely the function, not nested inside of any sort of script tag because there is no need to specify that the language is Javascript.
 
Joined
Jul 4, 2023
Messages
609
Reaction score
81
is there anyway i can get around this in Firefox?
In Firefox, the console shows both what console.log() prints and the value returned by the expression.
The function console.log() always returns undefined.
Because of this, Firefox displays undefined after the logged message.
This is normal behavior and not a JavaScript error.
Other browsers, like Chrome, usually hide the return value, which is why tutorials look different.
You can safely ignore undefined or type the variable name directly in the console to see its value.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,348
Messages
2,571,451
Members
48,795
Latest member
Lonell Lee

Latest Threads

Top