DEV Community

P Mukila
P Mukila

Posted on

I Learned Today: 🧠Understanding Local and Global Variables, DOM Using JavaScript, Linking External JavaScript to HTML.

When you're coding in JavaScript, variables are everywhere. But where and how you declare them determines whether they're local or global—and this can make or break your application.

Let’s break it down.

  1. 🌍 What is a Global Variable?

    A global variable is a variable declared outside of any function or block. It is accessible from anywhere in your code.
    📌 Example:

let globalMessage = "Hello, world!";

function greet() {
    console.log(globalMessage); // Accessible here
}

greet();
console.log(globalMessage); // Also accessible here

Enter fullscreen mode Exit fullscreen mode

In the example above, globalMessage is declared outside the greet() function, so it’s available globally.
⚠️ Why Global Variables Can Be Risky:

  • They can be accidentally overwritten.

  • They persist throughout the program, consuming memory.

  • In large codebases, they can cause naming conflicts.

🔒 What is a Local Variable?

A local variable is declared inside a function or block and can only be accessed from within that function or block.
📌 Example:

function greet() {
    let localMessage = "Hi!";
    console.log(localMessage); // Works
}

greet();
console.log(localMessage); // ❌ Error: localMessage is not defined

Enter fullscreen mode Exit fullscreen mode

The variable localMessage is local to the greet() function, so trying to access it outside throws an error.
🆚 Global vs Local: A Quick Comparison
Feature Global Variable Local Variable
Scope Entire script (or window) Only within the block/function
Accessibility Anywhere in the code Limited to function/block
Risk of Conflict High Low
💡 A Note on var, let, and const

var is function-scoped.

let and const are block-scoped.
Enter fullscreen mode Exit fullscreen mode

Example:

function testScope() {
    if (true) {
        var x = 10;
        let y = 20;
        const z = 30;
    }
    console.log(x); // ✅ var: accessible
    console.log(y); // ❌ let: not accessible
    console.log(z); // ❌ const: not accessible
}
Enter fullscreen mode Exit fullscreen mode

🧠 Best Practices

  • Avoid global variables unless absolutely necessary.

  • Use let and const instead of var to reduce bugs.

  • Always declare variables with a clear scope in mind.

2.📖DOM manipulation using JavaScript

The DOM (Document Object Model) is your gateway to making web pages interactive with JavaScript. It’s how you can read, update, add, or delete HTML elements dynamically.

Let’s dive into the essentials of DOM manipulation using JavaScript.

📖 What is the DOM?

When a web page loads, the browser creates a DOM — a tree-like structure of all elements in the page.

For example, this HTML:

<body>
  <h1>Hello</h1>
  <p id="intro">Welcome to the page!</p>
</body>
Enter fullscreen mode Exit fullscreen mode

Becomes this DOM tree:


document
 └── html
      └── body
           ├── h1
           └── p#intro
Enter fullscreen mode Exit fullscreen mode

JavaScript can access and manipulate any of these elements using the DOM API.

🛠️ Accessing DOM Elements

You can select elements using different methods:
1.getElementById

let intro = document.getElementById("intro");
Enter fullscreen mode Exit fullscreen mode

2.getElementsByClassName

let items = document.getElementsByClassName("item");
Enter fullscreen mode Exit fullscreen mode

3.getElementsByTagName

let paragraphs = document.getElementsByTagName("p");
Enter fullscreen mode Exit fullscreen mode

4.querySelector (CSS-style selector)

let heading = document.querySelector("h1"); // First h1
let introText = document.querySelector("#intro");

Enter fullscreen mode Exit fullscreen mode

5.querySelectorAll

let allParagraphs = document.querySelectorAll("p");
Enter fullscreen mode Exit fullscreen mode

✅ 3. Link an External JavaScript File

This is the best practice because it keeps your HTML clean and separates content from logic.

🧩 Example:

index.html

<!DOCTYPE html>
<html>
<head>
  <title>My Web Page</title>
</head>
<body>
  <h1>Hello World!</h1>

  <!-- Link to external JavaScript file -->
  <script src="script.js"></script>
</body>
</html>

script.js

console.log("JavaScript file is linked!");
Enter fullscreen mode Exit fullscreen mode
🔁 Make sure the JavaScript file (script.js) is in the same folder, or adjust the path accordingly.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)