MSDN Documentation

Your Gateway to Microsoft Technologies

Introduction to JavaScript

Welcome to this introductory tutorial on JavaScript! JavaScript is a powerful, versatile, and widely-used programming language that enables you to create dynamic and interactive web content. It's an essential skill for any web developer.

What is JavaScript?

Originally developed by Netscape as LiveScript, JavaScript is a high-level, interpreted programming language that conforms to the ECMAScript specification. It's primarily known for its role in web browsers, where it executes on the client-side to manipulate the Document Object Model (DOM), handle events, and make asynchronous requests.

Beyond the browser, JavaScript has gained immense popularity for server-side development with Node.js, mobile app development with frameworks like React Native, and even desktop applications.

Why Learn JavaScript?

Your First JavaScript Code

Let's start with a simple example. You can write JavaScript directly within your HTML file using the <script> tag.

Example 1: Displaying an Alert


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My First JS</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <script>
        alert("Welcome to JavaScript!");
    </script>
</body>
</html>
            

When you open this HTML file in a browser, you'll see a pop-up box with the message "Welcome to JavaScript!".

Example 2: Modifying HTML Content

JavaScript can interact with HTML elements. Here's how you can change the content of an element:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dynamic Content</title>
</head>
<body>
    <p id="myParagraph">This text will be changed.</p>
    <button onclick="changeText()">Change Text</button>

    <script>
        function changeText() {
            document.getElementById("myParagraph").innerHTML = "The text has been updated by JavaScript!";
        }
    </script>
</body>
</html>
            

Clicking the "Change Text" button will update the content of the paragraph with the ID "myParagraph".

Key Concepts Introduced:

  • <script> tag for embedding JavaScript.
  • alert() function for simple pop-up messages.
  • DOM manipulation: finding an element by its ID (document.getElementById()) and changing its content (.innerHTML).
  • Event handling: using the onclick attribute to trigger a JavaScript function.

Next Steps

This is just the beginning. In subsequent tutorials, we'll delve deeper into JavaScript's core concepts, including variables, data types, operators, control structures (like if/else and loops), functions, objects, arrays, and how to work with web APIs.

Keep practicing, and don't hesitate to experiment!

Continue to: Advanced JavaScript Concepts