-4

Why and when do we use this code.

console.log('for example : Program starts here');

Is it used to just take small notes?

David Thibault
  • 8,638
  • 3
  • 37
  • 51
Hadi
  • 1
  • 1

3 Answers3

2

It's used for printing messages/values to the javascript console (in Chrome it's under View -> Developer -> JavaScript Console). See here for the docs.

Buddy
  • 10,874
  • 5
  • 41
  • 58
0

You can use it to output some information in the browser's console. Usually it is done for debugging purposes.

toskv
  • 30,680
  • 7
  • 72
  • 74
0

It is useful for printing out debugging information in the browsers console while developing. In Chrome hit Ctrl + Shift + I to open the developer tools and switch to the Console to view output.

A developer may use this to verify that a line of JS is actually being reached, ie Console.log( "Reached Here" ); or to verify that a variable holds the value expected. Console.log( "Value of x is: " + x );

It is also very useful if you are writing JS for the server side using Node.js. This will allow you to log requests to the server and other useful information.

Console.log should probably be limited on the client side though, IE in real websites, especially if they were used for debugging as it may reveal site vulnerabilities. I have also seen Console.log cause issues in some versions of Internet Explorer.

Unome
  • 6,750
  • 7
  • 45
  • 87
barnesm999
  • 421
  • 3
  • 13