JavaScript Hello World

To create a “Hello, World!” program in JavaScript, you can use the following code:

console.log("Hello, World!");
Code language: JavaScript (javascript)

To run this code, you can either include it in an HTML file using a <script> tag and open the file in a web browser, or you can run it using a JavaScript runtime environment, such as Node.js.

In a Browser

Here’s an example of how you can include this code in an HTML file:

<html> <body> <script> console.log("Hello, World!"); </script> </body> </html>
Code language: HTML, XML (xml)

Save this code to an HTML file and open it in a web browser. You should see the “Hello, World!” message printed to the JavaScript console in your web browser’s developer tools.

Using Node.js

To run the “Hello, World!” JavaScript code using Node.js, you will need to have Node.js installed on your computer. You can download and install Node.js from the official website: https://nodejs.org/

Once you have Node.js installed, you can create a file called hello.js and paste the following code into it:

console.log("Hello, World!");
Code language: JavaScript (javascript)

Save the file and open a command prompt or terminal window. Navigate to the directory where you saved the hello.js file and run the following command:

node hello.js
Code language: CSS (css)

This will run the code in the hello.js file using Node.js. You should see the “Hello, World!” message printed to the terminal window.

Alternatively, you can run the code directly from the command line by passing the code as an argument to the node command, like this:

node -e "console.log('Hello, World!')"
Code language: JavaScript (javascript)

This will run the code directly without saving it to a file first. You should see the same “Hello, World!” message printed to the terminal window.

What is Hello World?

“Hello, World!” is a simple program that is often used to introduce a programming language or to verify that a programming environment is set up correctly. The program typically just outputs the text “Hello, World!” to the screen or console.

The use of the phrase “Hello, World!” as a test program dates back to 1973, when Brian Kernighan and Dennis Ritchie included it in their book “The C Programming Language”. Since then, it has become a common convention for learning a new programming language.

The purpose of the “Hello, World!” program is to provide a minimal working example of a program in a given language. This allows the programmer to verify that they have installed the language and its associated tools correctly and that they are able to write, compile, and run a simple program.

References

https://en.wikipedia.org/wiki/%22Hello,_World!%22_program


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *