JavaScript

Is There a PHP Echo/Print Equivalent in JavaScript

As we know in PHP, the echo and print functions are used for displaying output on the screen. So, when a user starts coding with JavaScript, they will definitely be curious about how to print the output on the screen in JavaScript. JavaScript is commonly used for building dynamic web applications which offer multiple predefined methods for performing tasks.

This article will describe the JavaScript methods equivalent to the echo/print method in PHP.

Is There a PHP echo/print Equivalent in JavaScript?

Yes, in JavaScript, there are various predefined methods to show output to the console or the HTML page, which are listed below:

Solution 1: Use the “console.log()” Method in JavaScript as Equivalent to the echo/print in PHP

The “console.log()” method in JavaScript allows you to print text to the browser’s console and is equivalent to PHP’s echo and print functions.

Example

Call the “console.log()” method to print the message on the console:

console.log("Welcome To Linuxhint");

The message has been successfully displayed on the console using the JavaScript “console.log()” method:

Solution 2: Use the “document.write()” Method in JavaScript as Equivalent to the echo/print in PHP

Use the “document.write()” method in JavaScript that is equivalent to the echo/print function in PHP. It displays the text on the web page. This method is frequently utilized for adding dynamic content to a web page.

Example

Pass the message to the “document.write()” method to print on the web page:

document.write("Welcome To Linuxhint");

It can be seen that the message has been successfully printed on the web page:

Solution 3: Use the “document.appendChild()” Method in JavaScript as Equivalent to the echo/print in PHP

Utilize the “document.appendChild()” method to print the text on the website using JavaScript and it is equivalent to the PHP’s echo or print functions. This method is used for adding new elements to the HTML document, such as <p> or <div> elements, and adding them to the document as a child of an existing element.

Example

Create a <p> tag element using the “create Element()” method and store the reference in the variable “text”:

var text = document.createElement("p");

Use the “innerText” attribute to assign the text for displaying on the web page:

text.innerText = "Welcome To Linuxhint";

Now, append the element in the HTML document using the “appendChild()” method:

document.body.appendChild(text);

Output

Solution 4: Use the “innerHTML” Attribute in JavaScript as Equivalent to the echo/print in PHP

You can also utilize the “innerHTML” property in JavaScript as equivalent to the echo/print functions in PHP. It permits to access or changes an HTML element’s content. This property is commonly used to update the content dynamically in response to user input or other events.

Example

Create an <p> element in HTML where we will change the text:

<p id="text">Print Message Here</p>

In a JavaScript file, get the reference of the <p> tag using the “getElementById()” method:

var elem = document.getElementById("text");

Utilize the “innerHTML” property to assign a new message for displaying on the web page dynamically:

elem.innerHTML = "Welcome To Linuxhint";

The message will be updated dynamically:

We have provided all the possible solutions for printing text on the web page using JavaScript equivalent to the echo/print function in PHP.

Conclusion

In JavaScript, there are various predefined methods to show output or text to the console or the HTML page including the “console.log()” method, “document.write()” method, “document.appendChild()” method or the “innerHTML” attribute. This article described the methods in JavaScript that are equivalent to the echo/print method in PHP.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.