Here's a brief overview of important JavaScript concepts, along with sample code snippets for each:
1. Hoisting
Hoisting refers to JavaScript's behavior of moving declarations (variables and functions) to the top of their scope before code execution.
console.log(a); // undefined due to hoisting
var a = 5;
2. Single Threaded
JavaScript runs in a single thread, meaning it can only execute one task at a time.
3. Synchronous
Synchronous code is executed line by line, with each operation blocking the next one until it completes.
console.log("First");
console.log("Second");
4. Function Invocation
This is the process of calling or executing a function.
function greet() {
console.log("Hello");
}
greet(); // Function invocation
5. Variable Environment
The context in which variables are stored and accessed, typically inside a scope (global, function, or block).
6. Scope
Scope determines the visibility and lifetime of variables and functions. In JavaScript, there are function scope, block scope, and global scope.
if (true) {
let x = 5; // Block scope
}
console.log(x); // Error: x is not defined
7. Asynchronous
Asynchronous code allows other operations to continue while waiting for a task to complete.
setTimeout(() => {
console.log("Asynchronous");
}, 1000);
