println JS alternative available? [SOLVED]


NodeJS

Is there an alternative for Java System.out.println() in JavaScript?

Whenever we code, we usually need a form of feedback from our program at some point. It can be to track the progress of our program or provide the result of a computation, etc. There are multiple circumstances where we require such functionality and the most fundamental way for a program to do this is to print a message containing whatever message we are trying to pass across.

Every programming language has an implementation of the printing function and if you are familiar with java you know of the System.out.println() which takes an argument and prints that argument.

If you recently picked up Javascript and were wondering what the equivalent of System.out.println()it is important to understand what exactly System.out.println() does.

 

What System.out.println() does?

To put it simply the System.out.println() takes an argument and prints the argument. Looking closely at, it follows the syntax objectname.methodname() which is because Java is a high-level classed based language.

For a further breakdown, the System stands as the class that provides lots of functionalities and holds important fields and methods which include standard input, standard output, and error output. The field that provides the functionality  standard output is out and is an instance of a static PrintStream modifier (or type).

Finally, the println() is a method of the PrintStream class that prints the argument it is passed and adds the escape character at the end of the output.

To print a statement, the below code will work for you in Java.

System.out.println("Printing Large Numbers")

Output:

Printing Large Numbers

 

The JavaScript equivalent of System.out.println() is console.log()

JS is a high-level and uses prototype-based object orientation to work, and it does have a method to print out statements or expression results that it’s passed, and that method is console.log().

console.log() is a method that takes an argument and outputs the argument (or the result of the argument) to the web console. The argument can be a string, number, boolean, or expression result to output.

 

The console object

Everything within JS is an object, and in some ways, the console.log() leans toward the way Java’s System.out.println() works.

If you noticed, we used screen phrasing when talking about Java’s println() method, but within JS, the phrasing will be different because everything we print is sent to the console.

A console is an object that allows us to access the browser’s debugging console, and its workings differ across different browsers. However, there are consistent features across all browsers that the console object provides to us.

The big job of the console object is logging (or displaying) different information, which is important to the debugging process, and one of the methods bound to it is the log() method

 

The log() method

To output (or print) text to the console, there are different methods bound to the console object that can help with that depending on how the output will be styled. However, for typical printing, the log() method is sufficient and equivalent to the println() method in Java. As a method, it prints the arguments passed to the web console, and the argument can be any JS object from strings to numbers. Also, it adds an escape character as well.

To print a string, the below example is helpful

console.log("Printing Large Numbers")

Output:

Printing Large Numbers

You can also print numbers and objects, and pass multiple arguments

const obj = {
    name: "John",
    age: 34,
    job: "Developer"
}

const salary = 120.6

console.log(obj, salary)

Output:

{ name: 'John', age: 34, job: 'Developer' } 120.6

 

Summary

If you are coming from Java to the beautiful language of JavaScript, understanding the equivalent of System.out.println() is a good starting point because it’s often the first means of debugging your code when there is an issue.

However, there are distinctions in the way both languages behave under the hood, and it’s important to know that and delve deeper into the ways to print (or log) different statements, expressions, or data types. There are other methods that help with printing text such as info() depending on how you want to present the text.

 

Further Reading

console - Web APIs | MDN (mozilla.org)
console.log() - Web APIs | MDN (mozilla.org)

 

Olorunfemi Akinlua

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment