Master JavaScript fundamentals with this beginner-friendly guide. Learn about variables, functions, user input, DOM manipulation, and external scripts through practical examples and interactive exercises.
prompt()document.write()<script>
    document.write("Hello World Wide Web");
</script>
alert()<script>
    alert('Hello, world!');
</script>
Goal: Learn where and how to place JavaScript in HTML.
<body><body>
    <h1>Hello World</h1>
    <script>
        alert("This runs from inside the body!");
    </script>
</body>
<body><body>
    <!-- HTML Content -->
    <script src="script.js"></script>
</body>
defer<head>
    <script src="script.js" defer></script>
</head>
Goal: Learn to declare and use variables.
<script>
    var str = 'Hello world';
    document.write(str + "<br>");
    var a = 5, b = 10;
    var c = a + b;
    document.write("Sum: " + c + "<br>");
    var myVar = 1;
    myVar = "Allah is the Greatest";
    document.write("<h1>" + myVar + "</h1>");
</script>
Goal: Learn to create and call functions.
alert()<script>
    function greet() {
        alert("Welcome to JavaScript!");
    }
    greet();
</script>
<script>
    function multiply(num1, num2) {
        return num1 * num2;
    }
    alert("Product: " + multiply(5, 6));
</script>
prompt()<script>
    function doShowName() {
        var name = prompt("What is your name?");
        alert(name + ", you just did something awesome!");
    }
</script>
<button onclick="doShowName()">Click Here</button>
prompt() and user interaction.π onclick Event (JavaScript)
The onclick event in JavaScript runs when a user clicks on an HTML elementβsuch as a button, link, or image. Itβs commonly used to perform actions like submitting forms, showing messages, or toggling features.
π For a detailed explanation, see:
π π±οΈ What is the onclick Event in JavaScript?
π π What are Events in JavaScript?
Goal: Modify HTML using JavaScript.
innerHTML<h1 id="demo">..............</h1>
<script>
    document.getElementById("demo").innerHTML = "Ya ALLAH!";
</script>
Goal: Organize JavaScript in separate files.
<h1 id="demo">ALLAH</h1>
<form>
    <input type="button" value="Click" onclick="msg()" />
</form>
<script src="scripts/hello.js"></script>
scripts/hello.jsfunction msg() {
    document.getElementById("demo").innerHTML = "Ya ALLAH!....";
}
For real-world JavaScript examples, See Real-World JavaScript Examples for Beginners