DEV Community

Shawn2208
Shawn2208

Posted on

JavaScript dumbed down Part three. control flow, basic functions, and interactions.

Introduction:
Understanding the fundamentals of Javascript is the key to building dynamic and interactive web applications, we'll dive into to the core concepts of control flow, functions and interactions with the (DOM) Document Object Model to empower you on your coding journey.

  1. Control Flow and Conditional Statements:

Control flow determines the sequence in which code executes. Conditional statements allow your code to make decisions based on conditions.

let age = 18;

if (age >= 18) {
    console.log("You're an adult.");
} else {
    console.log("You're a minor.");
}

Enter fullscreen mode Exit fullscreen mode

In this analogy, the if statement is like a decision point where you decide whether someone is an adult or a minor based on their age. If the condition is met (age is greater than or equal to 18), you let them pass as an adult. Otherwise, they take the "minor" route.

  1. Nested if Statements and Complex Decisions: Complex decisions are managed through nested if statements.
let temperature = 25;

if (temperature > 30) {
    console.log("It's hot outside!");
} else if (temperature >= 20) {
    console.log("The weather is pleasant.");
} else {
    console.log("It's a bit chilly.");
}

Enter fullscreen mode Exit fullscreen mode

Messages are printed based on the temperature range.

  1. Switch Statement for Multiple Values: Switch statements simplify handling multiple variable values.
let day = "Wednesday";
let message = "";

switch (day) {
    case "Monday":
        message = "Back to work!";
        break;
    case "Friday":
        message = "Weekend is near!";
        break;
    default:
        message = "Just another day.";
}

console.log(message);

Enter fullscreen mode Exit fullscreen mode

In this scenario, the switch statement acts as a menu of days. If the day matches "Monday," you order the "Back to work!" option. If it's "Friday," you choose "Weekend is near!" Otherwise, you just go with "Just another day."

  1. Functions for Structured Code: Functions encapsulate code for reuse.
function greet(name) {
    console.log("Hello, " + name + "!");
}

greet("Alice"); // Outputs: Hello, Alice!
greet("Bob");   // Outputs: Hello, Bob!

Enter fullscreen mode Exit fullscreen mode

Functions are like mini-scripts that you can call upon whenever you need them. They package a set of instructions together, allowing you to reuse the same logic without writing it out every time.
Consider the greet function as a personalized greeting generator. You provide a name as an input (parameter), and the function outputs a tailored greeting.

  1. Interacting with the DOM: The DOM represents the structure of a webpage, and JavaScript allows you to interact with it.
<!DOCTYPE html>
<html>
<head>
    <title>DOM Interaction</title>
</head>
<body>
    <button id="myButton">Click Me</button>
    <script>
        const button = document.getElementById("myButton");
        button.addEventListener("click", function() {
            alert("Button clicked!");
        });
    </script>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Conclusion:
By mastering control flow, functions, and DOM interactions, you're equipped to create responsive and engaging web applications. JavaScript's power lies in its ability to make decisions, structure code, and interact with the DOM. Now, start experimenting and combining these concepts to build your own interactive web experiences!

Next I will be diving further into JavaScript a whole article about Arrays & Loops, Array methods and examples of how you would use an array & loops in a real life scenario.

Top comments (0)