How to use JavaScript Console Log Like a Pro!

Consolelog

What is JavaScript Console log?

The Console.log() is a JavaScript function. It is used to print any kind of declared variable. Moreover it helps the user to view the output of JavaScript Snippet on user end.

Syntax :

Console.log("VisitGIS");

Parameters: It accepts all types of data types which can be string, number, array or object.

Return value: It returns the value of the parameter given.

Is console log Java or JavaScript?

The console.log is a function of JavaScript. There is no such function in Java. System.out.println() is a similar method in Java.

So remember, if somebody asks if console.log belongs to Java or JavaScript. Please don’t mix its origin. It is a function of JavaScript. There is no such method in Java with this name.

We may use the System.out.println() methods to display messages to the console as JavaScript in Java.

Console.log in Java

The println() is equal to console.log() in Java.

Lets understand with example

 
public class Javaprintln{
    public static void main(String[] args){
        System.out.println("Hello Consolelog, I am testing you in Java");
    }
}

Output is :

Hello Consolelog, I am testing you in Java

How to use the custom console.log method in Java?

public class CustomConsolelog{
    public static void main(String[] args){
        Console.log(" Hello Consolelog, I am testing custom consolelog in Java ");
    }
}    
class Console{
    public static void log(Object obj) {
        System.out.println(obj);
    }
}

Output:

Hello Consolelog, I am testing custom consolelog in Java

What is the purpose of Console log?

The console.log() is a function of JavaScript. It informs the developers what the code is performing.

However, It alters the user about the code warning, errors or outputs results. we should not take it in place of an interactive debugger, when it comes time to debug the code.

How do I use JavaScript Console log?

It is very simple to call this function in a js file or use it in the browser developer console.

Steps

  1. Create one JavaScript File .js file and call this file in HTML.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavascriptTuts</title>
</head>
<body>
    <div class="newclass">JAVA SCRIPT PRACTICE</div>
</body>
 
 <script src="./Codefiles/JS1.js"></script>
</html>
  1. Write below thing in js file JS1.js

console.log('I am in console') //string

  1. Execute this project in Browser.
  2. Press F12 and click on the console tab.
Console.log
  1. Output
Console.log

How do I debug JavaScript?

 

The debugger keyword stops the execution of JavaScript, and calls (if available) the debugging function.

This has the same function as setting a breakpoint in the debugger.

If no debugging is available, the debugger statement has no effect.

More Importantly the debugger turned on, this code will stop executing before it executes the third line.

<!DOCTYPE html>
<html>
<body>
 
<h2>JavaScript Debugger</h2>
 
<p id="demo"></p>
 
<p>With the debugger turned on, the code below should stop executing before it executes the third line.</p>
 
<script>
let x = 15 * 5;
debugger;
document.getElementById("demo").innerHTML = x;
</script>
 
</body>
</html>

Examples of Console log

Pass String as an argument

console.log(‘I am in console’) //string

Passing Integer as an Argument

console.log(23+56) //integers

Passing digits as an argument

console.log(56) //digits

Passing Boolean as an argument

console.log(true) //boolean

Passing Array as an argument

console.log([2,3,5,8]) //array

Passing Object as an argument

console.log({Umair : “this”, grade : “A”}) //Object

Passing table as an argument

console.table({Name : “Umair”, grade : “A”})  //log .table

How to give a warning using the console?

console.warn(‘this is warning’) //warning

How to clear the console using the clear function?

console.clear() //clear the console results

How to check code runtime using console?

console.timeEnd(“string”) //check code run time

How to use the assert method in the console?

console.assert(age>200,”This is not possible today”);

Conclusion

Console.log is a function of JavaScript. It helps to print output in the console for user display. However, it is useful in debugging and most importantly assist to identify bugs fast enough in code.

Leave a Comment