<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>getElementById() Example</title>
    </head>
    <body>
        <p id="hello">Welcome! Nice to meet you!</p>
        <script>
            const changeColor = newColor => {
                const element = document.getElementById("hello"); // Finds the element with id="hello".
                element.style.color = newColor;
            }
        </script>
        <button type="button" onclick="changeColor('red');">Change to Red</button>
        <button type="button" onclick="changeColor('blue');">Change to Blue</button>
    </body>
</html>
Rendered Output Try clicking the button!
document.getElementById(id);
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Error When the Target Element Does Not Exist</title>
    </head>
    <body>
        <button type="button" id="btn1">Button 1</button>
        <button type="button" id="btn2">Button 2</button>
        <button type="button" id="btn3">Button 3</button>
        <script>
            /* There is no element with id="btn-4". */
            const btn4Element = document.getElementById("btn-4"); // Finds the element with id="btn-4".
            
            // btn4Element becomes null instead of an element object.

            // Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
            btn4Element.addEventListener("click", () => {
                // ...
            });
        </script>
    </body>
</html>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Error When the Target Element Does Not Exist</title>
    </head>
    <body>
        <button type="button" id="btn1">Button 1</button>
        <button type="button" id="btn2">Button 2</button>
        <button type="button" id="btn3">Button 3</button>
        <script>
            /* There is no element with id="btn-4". */
            const eleBtn4 = document.getElementById("btn-4"); // Finds the element with id="btn-4".

            // eleBtn4 becomes null instead of an element object.

            if (eleBtn4) {
                eleBtn4.addEventListener("click", () => {
                    // ...
                });
            } else {
                console.warn("No element with id='btn-4' was found.");
            }
        </script>
    </body>
</html>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Error When the Target Element Does Not Exist</title>
    </head>
    <body>
        <button type="button" id="btn1">Button 1</button>
        <button type="button" id="btn2">Button 2</button>
        <button type="button" id="btn3">Button 3</button>
        <script>
            /* There is no element with id="btn-4". */
            const btn4 = document.getElementById("btn-4"); // Finds the element with id="btn-4".

            // btn4 becomes null instead of an element object.

            try {
                btn4.addEventListener("click", () => {
                    // ...
                });
            } catch (error) {
                console.warn("No element with id='btn-4' was found.");
            }
        </script>
    </body>
</html>
<div id="user-id"></div>
getElementById("User-id");
getElementById("User-Id");
getElementById("USER-ID");
getElementById("user-id");
<div id="container">
    <div id="child"></div>
</div>
const container = document.getElementById("container");
// Finds the matching element and returns an Element object.
// 'container' is an 'Element object', not the 'document object'.

// Uncaught TypeError: container.getElementById is not a function
const child = container.getElementById("child");
const child = document.getElementById("child");