What is console.log?

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

    You use it to debug JavaScript code with either Firebug for Firefox, or JavaScript console in WebKit browsers.

    var variable;
    
    console.log(variable);
    

    It will display the contents of the variable, even if it is a array or object.

    It is similar to print_r($var); for PHP.

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

    An example - suppose you want to know which line of code you were able to run your program (before it broke!), simply type in

    console.log("You made it to line 26. But then something went very, very wrong.")
    
    0 讨论(0)
  • 2020-11-22 01:21

    console.log has nothing to do with jQuery. It is a common object/method provided by debuggers (including the Chrome debugger and Firebug) that allows a script to log data (or objects in most cases) to the JavaScript console.

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

    You can view any messages logged to the console if you use a tool such as Firebug to inspect your code. Let's say you do this:

    console.log('Testing console');
    

    When you access the console in Firebug (or whichever tool you decide to use to inspect your code), you will see whatever message you told the function to log. This is particularly useful when you want to see if a function is executing, or if a variable is being passed/assigned properly. It's actually rather valuable for figuring out just what went wrong with your code.

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

    Use console.log to add debugging information to your page.

    Many people use alert(hasNinjas) for this purpose but console.log(hasNinjas) is easier to work with. Using an alert pop-ups up a modal dialog box that blocks the user interface.

    Edit: I agree with Baptiste Pernet and Jan Hančič that it is a very good idea to check if window.console is defined first so that your code doesn't break if there is no console available.

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

    console.log has nothing to do with jQuery.

    It logs a message to a debugging console, such as Firebug.

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