[java] What is the equivalent of Java's System.out.println() in Javascript?

I am writing some tests for Javascript code and I need to dump some messages during the compile process when errors are encountered.

Is there any equivalent to Java's System.out.println() in Javascript?

P.S.: I also need to dump debug statements while implementing tests.

UPDATE

I am using a maven plugin on a file containing all merged tests:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                <phase>test</phase>
                <goals>
                    <goal>java</goal>
                </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>org.mozilla.javascript.tools.shell.Main</mainClass>
                <arguments>
                    <argument>-opt</argument>
                    <argument>-1</argument>
                    <argument>${basedir}/src/main/webapp/html/js/test/test.js</argument>
                </arguments>
            </configuration>
        </plugin>

UPDATE II

I tried console.log("..."), but I get:

js: "src/main/webapp/html/js/concat/tests_all.js", line 147:
uncaught JavaScript runtime exception: ReferenceError: "console" is not defined

The code I am testing is a set of functions (like in a library). I am using QUnit.

This question is related to java javascript compile-time println system.out

The answer is


Essentially console.log("Put a message here.") if the browser has a supporting console.

Another typical debugging method is using alerts, alert("Put a message here.")

RE: Update II

This seems to make sense, you are trying to automate QUnit tests, from what I have read on QUnit this is an in-browser unit testing suite/library. QUnit expects to run in a browser and therefore expects the browser to recognize all of the JavaScript functions you are calling.

Based on your Maven configuration it appears you are using Rhino to execute your Javascript at the command line/terminal. This is not going to work for testing browser specifics, you would likely need to look into Selenium for this. If you do not care about testing your JavaScript in a browser but are only testing JavaScript at a command line level (for reason I would not be familiar with) it appears that Rhino recognizes a print() method for evaluating expressions and printing them out. Checkout this documentation.

These links might be of interest to you.

QUnit and Automated Testing

JavaScript Unit Tests with QUnit


I'm using Chrome and print() literally prints the text on paper. This is what works for me:

document.write("My message");

There isn't one, at least, not unless you are using a "developer" tool of some kind in your browser, e.g. Firebug in Firefox or the Developer tools in Safari. Then you can usually use console.log.

If I'm doing something in, say, an iOS device, I might add a <div id="debug" /> and then log to it.


In java System.out.println() prints something to console. In javascript same can be achieved using console.log().

You need to view browser console by pressing F12 key which opens developer tool and then switch to console tab.


You can always simply add an alert() prompt anywhere in a function. Especially useful for knowing if a function was called, if a function completed or where a function fails.

alert('start of function x');
alert('end of function y');
alert('about to call function a');
alert('returned from function b');

You get the idea.


I'm also about to ask the same question. But from what I've learned from codeacademy.com below code is enough to display the output or text?

print("hello world")  

I found a solution:

print("My message here");

console.log().

Chrome, Safari, and IE 8+ come with built-in consoles (as part of a larger set of development tools). If you're using Firefox, getfirebug.com.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to compile-time

What is the equivalent of Java's System.out.println() in Javascript? C++ Get name of type in template Runtime vs. Compile time

Examples related to println

How to print multiple variable lines in Java What is the equivalent of Java's System.out.println() in Javascript? Division of integers in Java Print in new line, java difference between System.out.println() and System.err.println()

Examples related to system.out

Why is printing "B" dramatically slower than printing "#"? How does System.out.print() work? What is System, out, println in System.out.println() in Java What is the equivalent of Java's System.out.println() in Javascript? How can I make Java print quotes, like "Hello"? What's the meaning of System.out.println in Java? difference between System.out.println() and System.err.println() How to color System.out.println output? How to set a string's color