Before we dive deeper, let’s see how to run your very first JavaScript code!
<!DOCTYPE html>
<html>
<head>
<title>My First JS Script</title>
</head>
<body>
<h1>Welcome to JavaScript</h1>
<script>
alert('Hello, World!');
</script>
</body>
</html>
In the example above, the <script> tag contains JavaScript that displays a simple popup saying “Hello, World!” when you load the page. Easy, right?
Internal Script: As seen above, you can place JavaScript directly in an HTML document inside the <script>
tag.
External Script: You can also separate JavaScript into its own file, which helps keep your code clean and organized. Here’s how you would link an external JavaScript file:
<script src="myScript.js"></script>