Abstract representation of JavaScript functions, interconnected nodes and flowcharts, colorful code syntax flowing like digital rivers, dark background with neon blue and green accents, modern tech illustration, clean and professional, suitable for programming blog header
Javascript and React - Scripts

Creating JavaScript Functions: A Practical Guide

Learning the essentials of creating JavaScript functions is fundamental for every web developer. Functions are reusable blocks of code that perform specific tasks in your programs. In this guide, you’ll learn how to create JavaScript functions from scratch, work with variables, and build interactive features. Let’s explore a practical example of how to create a simple login function.

Understanding the Anatomy of Creating JavaScript Functions

Let’s look at a complete example of a user entry function:

function enter(){
    var area = document.getElementById('area');
    var text = prompt('Inform your name');
    
    if(text == '' || text == null){
        alert('Please, inform your name again.');
        area.innerHTML = 'Welcome ...';
    }else{
        area.innerHTML = 'Welcome ' + text;
    }
}

Understanding the components

Variables (var)

Variables store information that will be used by the function. In the example above, we have two important variables:

area: This variable captures an HTML element through getElementById('area'). The ID is defined inside a <div> tag in the HTML and works as a “location” in the code where we can dynamically insert or modify content.

text: Stores the value that the user types in the prompt(), which is a dialog box that requests text input.

The conditional structure (if/else)

The conditional block checks if the user actually typed something:

if(text == '' || text == null)

This line checks two conditions using the logical OR operator (||):

  • If the text is empty ('')
  • Or if the text is null (null)

When either of these conditions is true, the system displays an alert asking the user to try again and keeps the default message “Bem Vindo…” in the area.

The innerHTML property

innerHTML is used to modify the HTML content of an element. In the example:

  • Error case: area.innerHTML = 'Welcome...' keeps the generic message
  • Success case: area.innerHTML = 'Welcome ' + text personalizes the message with the user’s name

Note that without setting the innerHTML after the alert, the area’s text would be left without the three dots and without proper personalization.

Basic HTML structure

For the function to work, you need an HTML element with the corresponding ID:

<div id="area">Welcome...</div>
<button onclick="enter()">Enter</button>

Best Practices for Creating JavaScript Functions

When creating JavaScript functions, remember to:

  1. Give descriptive names to your functions and variables
  2. Validate user inputs to avoid errors
  3. Provide clear feedback through alerts or messages
  4. Keep the code organized inside <script> tags

How functions work in scripts

Functions are placed inside <script> tags in your HTML file or in separate JavaScript files. Inside these functions, you create variables (var) and conditional statements (if). The script tag serves as the container where all your JavaScript logic lives, connecting the backend functionality with the frontend interface.

Conclusion

Functions are powerful tools that allow you to create interactive and dynamic experiences on your web pages. Practice creating your own functions based on the specific needs of your project. As you gain experience, you’ll discover endless possibilities for making your websites more engaging and user-friendly!

Until next time! ; )