简体   繁体   中英

console.log doesn’t print anything but returns undefined

I have made a script to grab all the sentences I'm learning on duolingo into one single console.log output. It worked perfectly until recently, when console.log started behave in a strange way :

console.log("Hello\tWorld")
undefined

whatever I type in console.log doesn't get printed at all

console.log(document)
undefined

this only happens on this particular page : https://www.duolingo.com/practice (you need to be logged in)

Is it even a thing ? to block a function like console.log on your page, preventing it from printing any result at all?

is there a way to print something in the console despite that ?

If you type console in the Console, you'll see something like:

log: ƒ ()
    ...
    [[FunctionLocation]]: app-676b15de.js:formatted:54288

That indicates the console.log() function is being overridden by app-676b15de.js . If you look in that file, you'll see this:

window.console && fY.forEach(function(e) {
    var t = console[e];
    console[e] = function() {
        for (var e = [], n = 0; n < arguments.length; n++)
            e[n] = arguments[n];
        (k() || window.verbose) && Function.prototype.apply.apply(t, [console, e])
    }
})

The last line prevents console functions from executing unless k() or window.verbose are .

This means you can effectively re-enable console.log() by adding this to the top of your script:

window.verbose = true;

FYI, as of the time of this post, the Duolingo page is overriding:

console.log()
console.error()

but not:

console.info()
console.warn()

So, you could also use either of the last two functions in place of the first two without any other modifications.

No, it doesn't. console.log() is a void -return function, meaning that it does not return any values ( spec here ).

As a result, when you run this in Chrome's Console, the string (ie Hello\\tWorld ) is correctly logged to the console, but Chrome is also reporting the function's return value, which is undefined since console.log() does not return anything.

You can see in the screenshot below that Chrome is correctly logging Hello Word to the console, and then returning undefined :

在此处输入图片说明

"undefined" is nothing but the return of function you called. its printing the string But as it doesnt return anything so console flashes undefined.

try

function abc(){return 2}

now when you call abc() it will show 2 (not undefined)

I had the same problem in Chrome. Then I noticed that next to Filter, just above the console prompt, it said "Hide all", which suppresses the messages printed by console.log(). I changed that to "Default" and the problem went away. This has nothing to do with the return type, which as noted is void. It's simply Chrome filtering the output.

I used console.info instead of console.log .

The latter still doesn't return anything to this day on that particular page.

Try restoring the default settings and reloading the console. #document will reappear in the first line of the console window and you're all set.

Settings > Preferences > button at bottom of tab "Restore defaults and reload".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM