JavaScript – Run First Script

In this lesson, we will create and run our first script in JavaScript. We can display the data or you can say print something in JavaScript using:

  • document.write(): Used mostly for testing
  • innerHTML: Preferred in Real World
  • console.log(): For Debugging

document.write() in JavaScript

The document.write() is a method in JavaScript to print. It is mostly used for testing and not a preferred way. Using it on a web page loads the page slowly and is not considered a good approach.

Let us see an example to print in JavaScript using document.write():

Output

JavaScript document.write()

innerHTML in JavaScript

The innerHTML is a property to define the content in HTML. You can write into an HTML element using innerHTML. With that, the id attribute is also used to define that specific HTML element. This id is set in the following:

Let us see an example to implement the innerHTML in JavaScript:

Output

JavaScript innerHTML


NOTE: In this complete tutorial, we will use the best approach i.e., innerHTML, since it loads the web page faster than using document.write().


console.log() in JavaScript

The console.log() method is used in JavaScript to display the data in the console. It is mainly used for debugging purposes.

Let us see an example to print using the console.log():

Output

To view the output on the console, press F12 on the web browser. Then, click the Console tab as shown below:

JavaScript console.log()

JavaScript Data Types
JavaScript Type Conversion
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading