### Basic Concepts
1. **Variables**:
- **`var`**: Declares a variable, optionally initializing it to a value.
- **`let`**: Declares a block-scoped local variable, optionally initializing it
to a value.
- **`const`**: Declares a block-scoped, read-only named constant.
```javascript
var a = 10;
let b = 20;
const c = 30;
```
2. **Data Types**:
- **Primitive**: Number, String, Boolean, Null, Undefined, Symbol, BigInt.
- **Non-primitive**: Objects (Array, Function, etc.).
```javascript
let num = 100; // Number
let str = "Hello, World!"; // String
let isTrue = true; // Boolean
let obj = { name: "Alice", age: 25 }; // Object
let arr = [1, 2, 3]; // Array (special type of object)
```
3. **Operators**:
- Arithmetic: `+`, `-`, `*`, `/`, `%`
- Comparison: `==`, `===`, `!=`, `!==`, `>`, `<`, `>=`, `<=`
- Logical: `&&`, `||`, `!`
```javascript
let x = 5 + 10; // 15
let y = 5 > 3; // true
let z = (5 > 3) && (10 > 5); // true
```
4. **Functions**:
- Function declaration
- Function expression
- Arrow function
```javascript
// Function declaration
function add(a, b) {
return a + b;
}
// Function expression
const subtract = function(a, b) {
return a - b;
};
// Arrow function
const multiply = (a, b) => a + b;
```
### Intermediate Concepts
1. **Scope**:
- **Global Scope**: Variables declared outside any function.
- **Local Scope**: Variables declared within a function.
- **Block Scope**: Variables declared within a block (`{}`).
```javascript
let globalVar = "global";
function myFunction() {
let localVar = "local";
console.log(globalVar); // accessible
console.log(localVar); // accessible
}
console.log(globalVar); // accessible
console.log(localVar); // not accessible
```
2. **Closures**:
- Functions that have access to the parent scope, even after the parent function
has closed.
```javascript
function outerFunction() {
let outerVar = "outer";
function innerFunction() {
console.log(outerVar); // "outer"
}
return innerFunction;
}
const inner = outerFunction();
inner(); // "outer"
```
3. **Asynchronous JavaScript**:
- **Callbacks**: Functions passed as arguments to other functions.
```javascript
function fetchData(callback) {
setTimeout(() => {
callback("Data loaded");
}, 1000);
}
fetchData(data => {
console.log(data); // "Data loaded" after 1 second
});
```
- **Promises**: Represent the eventual completion (or failure) of an
asynchronous operation.
```javascript
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Promise resolved");
}, 1000);
});
promise.then(data => {
console.log(data); // "Promise resolved" after 1 second
}).catch(error => {
console.error(error);
});
```
- **Async/Await**: Syntactic sugar over promises, making asynchronous code look
synchronous.
```javascript
async function fetchData() {
let data = await new Promise((resolve) => {
setTimeout(() => {
resolve("Data loaded");
}, 1000);
});
console.log(data); // "Data loaded" after 1 second
}
fetchData();
```
### Advanced Concepts
1. **Prototype and Inheritance**:
- Objects in JavaScript can inherit properties from other objects, using a
prototype chain.
```javascript
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
console.log("Hello, " + this.name);
};
const alice = new Person("Alice");
alice.greet(); // "Hello, Alice"
```
2. **Modules**:
- Using `import` and `export` to organize code into modules.
```javascript
// math.js
export function add(a, b) {
return a + b;
}
// main.js
import { add } from './math.js';
console.log(add(2, 3)); // 5
```
3. **Event Loop**:
- Understanding how JavaScript handles asynchronous operations with the event
loop.
```javascript
console.log('Start');
setTimeout(() => {
console.log('Timeout');
}, 0);
Promise.resolve().then(() => {
console.log('Promise');
});
console.log('End');
```
The output order will be:
```
Start
End
Promise
Timeout
```
4. **Advanced Asynchronous Patterns**:
- **Generators**: Functions that can be paused and resumed.
```javascript
function* generatorFunction() {
yield 'First';
yield 'Second';
return 'Third';
}
const generator = generatorFunction();
console.log(generator.next().value); // "First"
console.log(generator.next().value); // "Second"
console.log(generator.next().value); // "Third"
```
- **Async Iterators**: Using `for await...of` with asynchronous data sources.
```javascript
async function* asyncGenerator() {
yield 'First';
yield 'Second';
yield 'Third';
}
(async () => {
for await (let value of asyncGenerator()) {
console.log(value);
}
})();
```
5. **Web APIs**:
- Interaction with the browser's built-in APIs like `fetch`, `localStorage`,
`DOM`, etc.
```javascript
// Fetch API
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data));
// Local Storage
localStorage.setItem('name', 'Alice');
console.log(localStorage.getItem('name')); // "Alice"
// DOM Manipulation
document.getElementById('myElement').textContent = 'Hello, World!';
```
### Further Learning Resources
- **Books**: "JavaScript: The Good Parts" by Douglas Crockford, "Eloquent
JavaScript" by Marijn Haverbeke.
- **Online Courses**: Codecademy, FreeCodeCamp, Udemy, Coursera.
- **Documentation**: [MDN Web
Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript), [ECMAScript]
(https://www.ecma-international.org/publications/standards/Ecma-262.htm).
This should give you a solid foundation and understanding of JavaScript, from
basics to advanced concepts. If you have any specific questions or need further
explanation on any topic, feel free to ask!