What is console.log?

前端 未结 22 2336
面向向阳花
面向向阳花 2020-11-22 00:41

What is the use of console.log?

Please explain how to use it in JavaScript, with a code example.

相关标签:
22条回答
  • 2020-11-22 01:00

    Places you can view the console! Just to have them all in one answer.

    Firefox

    http://getfirebug.com/

    (you can also now use Firefox's built in developer tools Ctrl+Shift+J (Tools > Web Developer > Error Console), but Firebug is much better; use Firebug)

    Safari and Chrome

    Basically the same.

    https://developers.google.com/chrome-developer-tools/docs/overview

    https://developer.apple.com/technologies/safari/developer-tools.html

    Internet Explorer

    Don't forget you can use compatibility modes to debug IE7 and IE8 in IE9 or IE10

    http://msdn.microsoft.com/en-us/library/ie/gg589507(v=vs.85).aspx

    http://msdn.microsoft.com/en-us/library/dd565628(v=vs.85).aspx

    If you must access the console in IE6 for IE7 use the Firebug Lite bookmarklet

    http://getfirebug.com/firebuglite/ look for stable bookmarklet

    http://en.wikipedia.org/wiki/Bookmarklet

    Opera

    http://www.opera.com/dragonfly/

    iOS

    Works for all iPhones, iPod touch and iPads.

    http://developer.apple.com/library/ios/ipad/#DOCUMENTATION/AppleApplications/Reference/SafariWebContent/DebuggingSafarioniPhoneContent/DebuggingSafarioniPhoneContent.html

    Now with iOS 6 you can view the console through Safari in OS X if you plug in your device. Or you can do so with the emulator, simply open a Safari browser window and go to the "Develop" tab. There you will find options to get the Safari inspector to communicate with your device.

    Windows Phone, Android

    Both of these have no console built in and no bookmarklet ability. So we use http://jsconsole.com/ type :listen and it will give you a script tag to place in your HTML. From then on you can view your console inside the jsconsole website.

    iOS and Android

    You can also use http://html.adobe.com/edge/inspect/ to access web inspector tools and the console on any device using their convenient browser plugin.


    Older browser problems

    Lastly older versions of IE will crash if you use console.log in your code and not have the developer tools open at the same time. Luckily it's an easy fix. Use the below code snippet at the top of your code:

     if(!window.console){ window.console = {log: function(){} }; } 
    

    This checks to see if the console is present, and if not it sets it to an object with a blank function called log. This way window.console and window.console.log is never truly undefined.

    0 讨论(0)
  • 2020-11-22 01:00

    A point of confusion sometimes is that to log a text message along with the contents of one of your objects using console.log, you have to pass each one of the two as a different argument. This means that you have to separate them by commas because if you were to use the + operator to concatenate the outputs, this would implicitly call the .toString() method of your object. This in most cases is not explicitly overriden and the default implementation inherited by Object doesn't provide any useful information.

    Example to try in console:

    >>> var myObj = {foo: 'bar'}
    undefined
    >>> console.log('myObj is: ', myObj);
    myObj is: Object { foo= "bar"}
    

    whereas if you tried to concatenate the informative text message along with the object's contents you'd get:

    >>> console.log('myObj is: ' + myObj);
    myObj is: [object Object]
    

    So keep in mind that console.log in fact takes as many arguments as you like.

    0 讨论(0)
  • 2020-11-22 01:00

    In java scripts there is no input and output functions. So to debug the code console.log() method is used.It is a method for logging. It will be printed under console log (development tools).

    Its is not present in IE8 and under until you open IE development tool.

    0 讨论(0)
  • 2020-11-22 01:04

    console.log logs debug information to the console on some browsers (Firefox with Firebug installed, Chrome, IE8, anything with Firebug Lite installed). On Firefox it is a very powerful tool, allowing you to inspect objects or examine the layout or other properties of HTML elements. It isn't related to jQuery, but there are two things that are commonly done when using it with jQuery:

    • install the FireQuery extension for Firebug. This, amongst other advantages, makes the logging of jQuery objects look nicer.

    • create a wrapper that is more in line with jQuery's chaining code conventions.

    This means usually something like this:

    $.fn.log = function() {
        if (window.console && console.log) {
            console.log(this);
        }
        return this;
    }
    

    which you can then invoke like

    $('foo.bar').find(':baz').log().hide();
    

    to easily check inside jQuery chains.

    0 讨论(0)
  • 2020-11-22 01:06

    There is nothing to do with jQuery and if you want to use it I advice you to do

    if (window.console) {
        console.log("your message")
    }
    

    So you don't break your code when it is not available.

    As suggested in the comment, you can also execute that in one place and then use console.log as normal

    if (!window.console) { window.console = { log: function(){} }; }
    
    0 讨论(0)
  • 2020-11-22 01:07

    It is used to log (anything you pass it) to the Firebug console. The main usage would be to debug your JavaScript code.

    0 讨论(0)
提交回复
热议问题