Here you will learn how to print 'Hello World' in JavaScript. JavaScript allows you to either include your code internally in an HTML file or externally in a different JavaScript file.
In this method, using the <script> tags, you write your JavaScript code directly into the HTML document.
With this technique, you create a separate JavaScript file with the .js extension and link it to the HTML file.
To add the path to the external JavaScript file, use the <script> tag with the src attribute.
Open your preferred code editor, such as Visual Studio Code. Make a file with the extension .html such as index.html.
- The first step is to open the file.
- Insert a
<script>tag within the<body>tag. - In the
<script>tag, write your JavaScript code using theconsole.logto observe the result. Example:
<body>
<script>
console.log("Hello, World!");
</script>
</body>- Save the file, then.
- Open a web browser and view it. To see the result, open the browser's developer console (such as using the browser's developer tools).
- First of all, create a JavaScript file(script.js) and open it.
- Write your JavaScript code directly in the file using
console.logfor output. Example:
console.log("Hello, World!");- Then save the JavaScript file.
- Create a new HTML file(index.html) and open it.
- Add a
<script>`` tag with thesrcattribute inside the` tag that points to the JavaScript file.
Example:
<body>
<script src="script.js"></script>
</body>- Then save the file.
- Open the HTML file in a web browser, or run the code by clicking the Live Server extension in Visual Studio Code. This will run the code in the browser. With this extension, you will not have to repeatedly refresh the page to see results. It syncs changes immediately after you save the file and displays them in the browser. To view the output, open the browser's developer console.
You can use console.log to see JavaScript output by following these simple steps either directly in an HTML file or using the external JavaScript file method.