1.scopes
2.Hoisting
3.Functions
4.Arrow Functions
5.Promises
6.Fetching API
7.Async/Await
1. Scopes π
Scope determines the accessibility of variables in JavaScript.
Types of Scope
- Global Scope π: Variables declared outside any function are accessible everywhere.
- Local Scope π: Variables declared inside a function are only accessible within that function.
-
Block Scope π§±: Introduced with
let
andconst
, variables declared inside{}
cannot be accessed outside. -
Function Scope π―: Variables declared using
var
inside a function are only accessible within that function.
2. Hoisting π
Hoisting is JavaScriptβs behavior of moving variable and function declarations to the top of their scope before execution.
Example
console.log(a); // undefined (not error)
var a = 10;
foo(); // β
Works! Function declarations are hoisted
function foo() {
console.log("Function Hoisted!");
}
// Function Expressions are **not** hoisted
bar(); // β Error: bar is not a function
var bar = function () {
console.log("Function Expression");
};
Note: Only function declarations are fully hoisted, but variables declared with
var
are hoisted withundefined
value.
3. Functions π
Function Declaration vs Function Expression
Feature | Function Declaration | Function Expression |
---|---|---|
Hoisted? | β Yes | β No |
Can be called before declaration? | β Yes | β No |
Syntax | function func() {} |
const func = function() {} |
Example
// Function Declaration
function greet() {
return "Hello!";
}
console.log(greet()); // β
Works!
// Function Expression
const greetExpression = function() {
return "Hello from expression!";
};
console.log(greetExpression()); // β
Works only after definition
4. Arrow Functions β‘οΈ
Arrow functions provide a more concise syntax for writing functions.
Syntax
const functionName = (parameters) => expression;
Example
// Traditional Function
function add(a, b) {
return a + b;
}
// Arrow Function Equivalent
const addArrow = (a, b) => a + b;
console.log(addArrow(3, 4)); // Output: 7
Key Differences
-
this
is lexically bound (inherits from the surrounding scope). -
Cannot be used as constructors (
new
keyword). -
No
arguments
object available.
5. Promises π€
A Promise is an object that represents the eventual completion or failure of an asynchronous operation.
States of a Promise
- Pending π‘ - Initial state, before the result is available.
- Fulfilled β - The operation was successful.
- Rejected β - The operation failed.
Why use Promises?
βοΈ Handle asynchronous operations efficiently.
βοΈ Avoid callback hell.
Example
const myPromise = new Promise((resolve, reject) => {
let success = true;
setTimeout(() => {
if (success) resolve("Promise resolved! β
");
else reject("Promise rejected! β");
}, 2000);
});
myPromise
.then(response => console.log(response)) // Runs if resolved
.catch(error => console.log(error)); // Runs if rejected
6. Fetching Data from an API π
Fetch is used to request data from a server.
Example
fetch("https://jsonplaceholder.typicode.com/posts/1")
.then(response => response.json()) // Convert response to JSON
.then(data => console.log(data)) // Log data
.catch(error => console.log("Error:", error));
It returns a Promise, which we handle using
.then()
and.catch()
.
7. Async/Await β³
async/await
makes handling asynchronous code cleaner and more readable.
Example
async function fetchData() {
try {
let response = await fetch("https://jsonplaceholder.typicode.com/posts/1");
let data = await response.json();
console.log(data);
} catch (error) {
console.log("Error:", error);
}
}
fetchData();
Why use async/await
?
βοΈ Avoids callback hell
βοΈ Improves readability
βοΈ Easier error handling using try...catch
π Summary
- Scope: Determines where variables are accessible.
-
Hoisting: Moves function declarations and
var
variables to the top before execution. - Function Declarations vs Expressions: Declarations are hoisted, expressions are not.
-
Arrow Functions: Shorter syntax, no
this
binding. -
Promises: Handle async operations with
.then()
and.catch()
. - Fetch API: Used for making network requests.
- Async/Await: Syntactic sugar over Promises for cleaner async code.
πΉ Master these concepts, and you're on your way to becoming a JavaScript pro! π
Top comments (0)