JavaScript
JavaScript
Introduction: All modern we browsers, Node.js as well as almost every other JavaScript
environment support writing message to a console using a suite of logging methods.
The most common of these methods is console.log (). In a browser environment, the
console.log () function is predominantly used for debugging purposes.
Getting Started: Console.log (“Hello, world!”);
This is because console.log () has no explicit return value.
Logging variables: Console.log () can be used to log variables of any kind; not only
strings. Just pass in the variable that you want to be displayed in the console, for
example: var foo = “bar”; console.log(foo);
If want more to log two or more values, simply separate them with commas.
Var thisVar = ‘first value’; var thatVar = ‘second value’; console.log (“thisVar:”, thisVar,
“and thatVar:”, thatVar);
Placeholders: Can use console.log () in combination with placeholders: var greet =
“Hello”, who = “World”; console.log (“%s, %s!”, greet, who);
Logging objects: Below we see the result of logging an object. This is often useful for
logging JSON responses from API calls. Console.log ( { ‘email’: ‘’, ‘groups’: {}, ‘id’ : 33,
‘ishiddeninui’; false, ‘issiteadmin’: false, ‘loginname’: ‘i:0#.w|virtualdomain\\user2’,
‘principaltype’:1, ‘title’: ‘user2’});
Logging HTML elements: You have the ability to log any element which exists within
the DOM. In this case we log the body element: console.log(document.body);
Because window is the global object, you can call also use the following shorthand:
alert(message);
So what does window.alert( )do? Well, let’s take the following example: alert (‘hello, world’);
The alert method is technically a property of window object, but since all window properties
are automatically global variables, we can use alert as a global variable instead of as a property
of window – meaning you can directly use alert () instead of window.alert( ).