To write your first code in JavaScript, you can start with a simple example that demonstrates the basics of the language. Here's a classic "Hello, World!" program in JavaScript:
Create an HTML file, and within the <script>
tags, write your JavaScript code. Open the HTML file in a web browser to see the output.
HTML file (example.html):
<!DOCTYPE html>
<html>
<head>
<title>My First JavaScript Code</title>
</head>
<body>
<h1>My First JavaScript Code</h1>
<script>
// JavaScript code
console.log("Hello, World! This is my first JavaScript code.");
</script>
</body>
</html>
Create a JavaScript file (example.js) and link it to an HTML file.
HTML file (index.html):
<!DOCTYPE html>
<html>
<head>
<title>My First JavaScript Code</title>
<script src="example.js"></script>
</head>
<body>
<h1>My First JavaScript Code</h1>
</body>
</html>
JavaScript file (example.js):
// JavaScript code in example.js
console.log("Hello, World! This is my first JavaScript code.");
When you open the HTML file in a web browser, open the developer console (usually by right-clicking and selecting "Inspect," then navigating to the "Console" tab) to see the output of your JavaScript code.
The console.log()
function in JavaScript is used to output text or other data to the console, allowing you to see the result of your code execution.
These basic examples introduce you to the process of writing and running JavaScript code. They demonstrate how to output a simple message in the console, which is a good starting point for learning the language.
Thank you.