DEV Community

Cover image for Day 02 : Script Loading Methods, Variables, Scopes, and Error Handling in JavaScript
Dev Pattani
Dev Pattani

Posted on

Day 02 : Script Loading Methods, Variables, Scopes, and Error Handling in JavaScript

Hey everyone! 🚀

Today, on day 2 of my JavaScript challenge, I explored script loading methods, variables, scopes, and error handling in JavaScript.

  1. Script Loading Methods:
    • JavaScript offers two main methods for loading scripts: internal and external loading.
    • Internal loading involves placing the script directly within the HTML file, while external loading refers to linking an external script file.

Internal Method

jS internal method

External Method

HTML File
jS external method
JS File
jS external method

  • Attributes like 'defer' and 'async' control the execution behavior of external scripts.
    • The 'defer' attribute ensures that the script is executed only after the HTML content is fully loaded.
    • The 'async' attribute allows the script to be loaded asynchronously, meaning it doesn't block the rendering of the page.defer vs async
  1. Types of Errors in JavaScript:

    • JavaScript code can encounter various types of errors, including syntax errors, runtime errors, and logic errors.
    • Syntax errors occur when the code violates the rules of the language.
    • Runtime errors occur during the execution of the code, such as trying to access an undefined variable.
    • Logic errors, also known as bugs, occur when the code does not produce the expected output due to flawed logic.
  2. Variables in JavaScript:

    • Variables in JavaScript are used to store and manipulate data.
    • Keywords like 'let', 'const', and 'var' are used to declare variables.
    • 'let' and 'const' are block-scoped, meaning they are limited to the block of code in which they are declared.
    • The 'var' keyword declares function-scoped variables that are accessible within the function in which they are defined.
  3. Scopes in JavaScript:

    • Scopes in JavaScript determine the accessibility of variables in different parts of the code.
    • Global scope: Variables declared outside of any function are in the global scope and can be accessed anywhere in the code.
    • Local scope: Variables declared inside a function are in the local scope and can only be accessed within that function.
    • Block scope: Introduced with 'let' and 'const', block scope restricts variable accessibility to the block of code in which they are defined (e.g., within loops or conditionals).

Understanding scopes is crucial for writing clean and efficient JavaScript code and avoiding variable conflicts.

In the upcoming days, we will explore more about variables and scopes.

Stay tuned for more updates on my JavaScript challenge journey! 🌟

Let's keep coding and learning together! 💻✨

Looking forward to sharing more insights with you all.

Happy coding! 🚀

DEV PATTANI

Top comments (0)