What is console.log?

前端 未结 22 2305
面向向阳花
面向向阳花 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:13

    It's not a jQuery feature but a feature for debugging purposes. You can for instance log something to the console when something happens. For instance:

    $('#someButton').click(function() {
      console.log('#someButton was clicked');
      // do something
    });
    

    You'd then see #someButton was clicked in Firebug’s “Console” tab (or another tool’s console — e.g. Chrome’s Web Inspector) when you would click the button.

    For some reasons, the console object could be unavailable. Then you could check if it is - this is useful as you don't have to remove your debugging code when you deploy to production:

    if (window.console && window.console.log) {
      // console is available
    }
    

提交回复
热议问题