Learn basic JavaScript

What is JavaScript?

JavaScript is a programming language used to add interactivity to web pages, such as dynamic content updates, animations, and form validations.

How to Add JavaScript

JavaScript can be added to a webpage in three ways:

  • Inline: Directly inside an element's onclick, onchange, etc.
  • Internal: Inside a <script> tag within the HTML.
  • External: Linked as a separate file using <script src="script.js"></script>.

Example: Internal JavaScript

Click the button below to see JavaScript in action:

JavaScript Basics

Here are some basic concepts:

  • Variables: Used to store data.
    let name = "John";
    const age = 25;
    var isStudent = true;
                
  • Functions: Blocks of reusable code.
    function greet() {
        alert("Hello, world!");
    }
                
  • Events: Actions like clicks or key presses.
    button.addEventListener('click', function() {
        alert('Button clicked!');
    });
                

Example Script

Here’s a simple script to display a message:

External JavaScript

To use external JavaScript, create a file named script.js and link it in the HTML:

<script src="script.js"></script>
    

In script.js, you can write your JavaScript code:

// Example script.js content
function showAlert() {
    alert("This is an external JavaScript file!");
}
    

Learning Resources