Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

JavaScript Tutorial

Introduction to JavaScript

JavaScript is a versatile programming language that allows you to implement complex features on web pages. It is one of the core technologies of the World Wide Web, alongside HTML and CSS.

Setting Up JavaScript

To start using JavaScript, you need to include it in your HTML file. You can write JavaScript directly inside the HTML file using the <script> tag.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Example</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <script>
        alert('Hello, World!');
    </script>
</body>
</html>
                

Variables and Data Types

Variables in JavaScript are used to store data values. You can declare a variable using var, let, or const.

<script>
    var name = 'John';
    let age = 30;
    const country = 'USA';
</script>
                

Functions

Functions are blocks of code designed to perform a particular task. You can define a function using the function keyword.

<script>
    function greet() {
        console.log('Hello, World!');
    }
    greet();
</script>
                

DOM Manipulation

The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the page so that programs can change the document structure, style, and content.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DOM Manipulation</title>
</head>
<body>
    <h1 id="title">Hello, World!</h1>
    <button onclick="changeText()">Change Text</button>
    <script>
        function changeText() {
            document.getElementById('title').innerText = 'Text Changed!';
        }
    </script>
</body>
</html>
                

Event Handling

Event handling in JavaScript allows you to execute code when events occur, such as when a user clicks a button, hovers over an element, or submits a form.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Event Handling</title>
</head>
<body>
    <button id="myButton">Click Me</button>
    <script>
        document.getElementById('myButton').addEventListener('click', function() {
            alert('Button Clicked!');
        });
    </script>
</body>
</html>
                

Asynchronous JavaScript

Asynchronous JavaScript allows you to execute code without blocking the main thread. This is useful for operations that take time, such as fetching data from a server.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Asynchronous JavaScript</title>
</head>
<body>
    <button onclick="fetchData()">Fetch Data</button>
    <div id="output"></div>
    <script>
        function fetchData() {
            fetch('https://jsonplaceholder.typicode.com/posts')
                .then(response => response.json())
                .then(data => {
                    document.getElementById('output').innerHTML = JSON.stringify(data, null, 2);
                });
        }
    </script>
</body>
</html>
                

Conclusion

This tutorial has covered the basics of JavaScript, including setting up JavaScript, variables, functions, DOM manipulation, event handling, and asynchronous JavaScript. There is a lot more to learn, but this should give you a solid foundation to get started with JavaScript.