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:
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:
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”:
Use the “innerText” attribute to assign the text for displaying on the web page:
Now, append the element in the HTML document using the “appendChild()” method:
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:
In a JavaScript file, get the reference of the <p> tag using the “getElementById()” method:
Utilize the “innerHTML” property to assign a new message for displaying on the web page dynamically:
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.