Programing

console.log은 (는) 무엇 이죠?

lottogame 2020. 4. 27. 07:52
반응형

console.log은 (는) 무엇 이죠?


사용은 console.log무엇입니까?

코드 예제와 함께 JavaScript에서 사용하는 방법을 설명하십시오.


jQuery 기능이 아니라 디버깅 목적의 기능입니다. 예를 들어 무언가가 발생하면 콘솔에 무언가를 기록 할 수 있습니다. 예를 들어 :

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

그러면 #someButton was clicked버튼을 클릭하면 Firebug의 "콘솔"탭 (또는 다른 도구 콘솔 (예 : Chrome의 웹 검사기))에 표시됩니다.

어떤 이유로 콘솔 개체를 사용하지 못할 수 있습니다. 그렇다면 프로덕션 환경에 배포 할 때 디버깅 코드를 제거 할 필요가 없으므로 유용합니다.

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

콘솔을 볼 수있는 장소! 하나의 답변으로 모든 것을 갖기 위해.

Firefox

http://getfirebug.com/

(이제 Firefox의 내장 개발자 도구 Ctrl + Shift + J (도구> 웹 개발자> 오류 콘솔)를 사용할 수 있지만 Firebug가 훨씬 좋습니다. Firebug를 사용하십시오)

사파리와 크롬

기본적으로 동일합니다.

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

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

인터넷 익스플로러

호환성 모드를 사용하여 IE9 또는 IE10에서 IE7 및 IE8을 디버깅 할 수 있음을 잊지 마십시오.

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

IE7의 IE6에서 콘솔에 액세스해야하는 경우 Firebug Lite 책갈피를 사용하십시오.

http://getfirebug.com/firebuglite/ 안정적인 북마크릿 찾기

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

오페라

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

iOS

모든 iPhone, iPod touch 및 iPad에서 작동합니다.

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

iOS 6에서는 기기를 연결하면 OS X의 Safari를 통해 콘솔을 볼 수 있습니다. 또는 에뮬레이터를 사용하여 Safari 브라우저 창을 열고 "개발"탭으로 이동하면됩니다. 여기에서 Safari 관리자가 장비와 통신 할 수있는 옵션이 있습니다.

윈도우 폰, 안드로이드

둘 다 콘솔이 내장되어 있고 북마크 기능이 없습니다. 따라서 http://jsconsole.com/ type : listen을 사용하면 HTML에 배치 할 스크립트 태그가 제공됩니다. 그때부터 jsconsole 웹 사이트에서 콘솔을 볼 수 있습니다.

iOS와 안드로이드

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.


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.


It will post a log message to the browser's javascript console, e.g. Firebug or Developer Tools (Chrome / Safari) and will show the line and file where it was executed from.

Moreover, when you output a jQuery Object it will include a reference to that element in the DOM, and clicking it will go to that in the Elements/HTML tab.

You can use various methods, but beware that for it to work in Firefox, you must have Firebug open, otherwise the whole page will crash. Whether what you're logging is a variable, array, object or DOM element, it will give you a full breakdown including the prototype for the object as well (always interesting to have a poke around). You can also include as many arguments as you want, and they will be replaced by spaces.

console.log(  myvar, "Logged!");
console.info( myvar, "Logged!");
console.warn( myvar, "Logged!");
console.debug(myvar, "Logged!");
console.error(myvar, "Logged!");

These show up with different logos for each command.

You can also use console.profile(profileName); to start profiling a function, script etc. And then end it with console.profileEnd(profileName); and it will show up in you Profiles tab in Chrome (don't know with FF).

For a complete reference go to http://getfirebug.com/logging and I suggest you read it. (Traces, groups, profiling, object inspection).

Hope this helps!


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(){} }; }

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.


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.


console.log has nothing to do with jQuery.

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


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.


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.


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.")

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.


Beware: leaving calls to console in your production code will cause your site to break in Internet Explorer. Never keep it unwrapped. See: https://web.archive.org/web/20150908041020/blog.patspam.com/2009/the-curse-of-consolelog


In early days JS debugging was performed through alert() function - now it is an obsolete practice.

The console.log() is a function that writes a message to log on the debugging console, such as Webkit or Firebug. In a browser you will not see anything on the screen. It logs a message to a debugging console. It is only available in Firefox with Firebug and in Webkit based browsers (Chrome and Safari). It does not work well in all IE releases.

The console object is an extension to the DOM.

The console.log() should be used in code only during development and debugging.

It’s considered bad practice that someone leaves console.log() in the javascript file on the production server.


If your browser supports debugging, you can use the console.log() method to display JavaScript values.

Activate debugging in your browser with F12, and select "Console" in the debugger menu.

Console in JavaScript. Try to fix, or "debug," a non-functioning JavaScript program, and practice using the console.log() command. There are shortcuts that is going to help you to access to the JavaScript console, based on the browser that you are using:

Chrome Console Keyboard Shortcuts

Windows: Ctrl + Shift + J
Mac: Cmd + Option + J

Firefox Console Keyboard Shortcuts

Windows: Ctrl + Shift + K
Mac: Cmd + Option + K

Internet Explorer Console Keyboard Shortcuts

F12 key

Safari Console Keyboard Shortcuts

Cmd + Option + C


console.log specifically is a method for developers to write code to inconspicuously inform the developers what the code is doing. It can be used to alert you that there's an issue, but shouldn't take the place of an interactive debugger when it comes time to debug the code. Its asynchronous nature means that the logged values don't necessarily represent the value when the method was called.

In short: log errors with console.log (if available), then fix the errors using your debugger of choice: Firebug, WebKit Developer Tools (built-in to Safari and Chrome), IE Developer Tools or Visual Studio.


I really feel web programming easy when i start console.log for debugging.

var i;

If i want to check value of i runtime..

console.log(i);

you can check current value of i in firebug's console tab. It is specially used for debugging.


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


Apart from the usages mentioned above, console.log can also print to the terminal in node.js. A server created with express (for eg.) can use console.log to write to the output logger file.


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.

참고URL : https://stackoverflow.com/questions/4743730/what-is-console-log-and-how-do-i-use-it

반응형