Day 02 : Script Loading Methods, Variables, Scopes, and Error Handling in JavaScript
Tags: webdev, javascript, beginners, programming
Published: 2024-04-08 18:00:43


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1bv7xex6o15mue1l2jw.png)

##External Method
   **_HTML File_**
![jS external method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4t6luc1ib3u89kdn2ejv.png)
   **_JS File_**
![jS external method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5c443a4nys8spocfekh.png)




   - 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dlmt0ja1rmiq16bhsn32.png)


2. **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.

3. **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.

4. **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**