[removed] How do I print a message to the error console?

前端 未结 18 2106
闹比i
闹比i 2020-12-04 04:52

How can I print a message to the error console, preferably including a variable?

For example, something like:

print(\'x=%d\', x);
相关标签:
18条回答
  • 2020-12-04 05:13

    If you are using Firebug and need to support IE, Safari or Opera as well, Firebug Lite adds console.log() support to these browsers.

    0 讨论(0)
  • 2020-12-04 05:15

    function foo() {
      function bar() {
        console.trace("Tracing is Done here");
      }
      bar();
    }
    
    foo();

    console.log(console); //to print console object
    console.clear('console.clear'); //to clear console
    console.log('console.log'); //to print log message
    console.info('console.info'); //to print log message 
    console.debug('console.debug'); //to debug message
    console.warn('console.warn'); //to print Warning
    console.error('console.error'); //to print Error
    console.table(["car", "fruits", "color"]);//to print data in table structure
    console.assert('console.assert'); //to print Error
    console.dir({"name":"test"});//to print object
    console.dirxml({"name":"test"});//to print object as xml formate

    To Print Error:- console.error('x=%d', x);
    

    console.log("This is the outer level");
    console.group();
    console.log("Level 2");
    console.group();
    console.log("Level 3");
    console.warn("More of level 3");
    console.groupEnd();
    console.log("Back to level 2");
    console.groupEnd();
    console.log("Back to the outer level");

    0 讨论(0)
  • 2020-12-04 05:17

    If you use Safari, you can write

    console.log("your message here");
    

    and it appears right on the console of the browser.

    0 讨论(0)
  • 2020-12-04 05:18

    To actually answer the question:

    console.error('An error occurred!');
    console.error('An error occurred! ', 'My variable = ', myVar);
    console.error('An error occurred! ' + 'My variable = ' + myVar);
    

    Instead of error, you can also use info, log or warn.

    0 讨论(0)
  • 2020-12-04 05:18

    Visit https://developer.chrome.com/devtools/docs/console-api for a complete console api reference

        console.error(object[Obj,....])\
    

    In this case, object would be your error string

    0 讨论(0)
  • 2020-12-04 05:24

    To answer your question you can use ES6 features,

    var var=10;
    console.log(`var=${var}`);
    
    0 讨论(0)
提交回复
热议问题