JavaScript Concepts
JavaScript Concepts
A callback function is a function that is passed as an argument to another function and is executed
after some operation has been completed. It allows functions to be executed asynchronously, which
is common in JavaScript, especially when dealing with tasks like API calls, reading files, or handling
events.
Example:
function greet(name) {
function processUserInput(callback) {
callback(name);
Scope
Scope in JavaScript refers to the visibility or accessibility of variables and functions in different parts
1. Global Scope: Variables declared outside of any function are in the global scope and can be
function printGlobalVar() {
2. Local Scope (Function Scope/Block Scope): Variables declared within a function or block {} are
Example:
function testScope() {
testScope();
Hoisting
Hoisting is a behavior in JavaScript where variable and function declarations are moved (or
"hoisted") to the top of their scope before the code execution. This means that variables and
Example:
sayHello();
function sayHello() {
console.log('Hello World!');
Closure
A closure is a function that remembers and has access to its own lexical scope, even when the
function is executed outside that scope. Closures allow inner functions to remember the
Example:
function outerFunction(outerVariable) {
};
newFunction('innerValue');
// Output: