Category: javascript
Functions
Published on 29 Jun 2026
Explanation
Functions are reusable blocks of code that perform a specific task and can accept input through parameters.
Code:
function greet(name) {
console.log('Hello ' + name);
}
greet('John');
Explanation
Functions can return values using the return statement.
Code:
function add(a, b) {
return a + b;
}
console.log(add(5, 3));
Explanation
Variables declared inside a function have local scope and are accessible only within that function.
Code:
function demo() {
let message = 'Hello';
console.log(message);
}
Explanation
Variables declared outside functions have global scope and can be accessed throughout the program.
Code:
let company = 'HackForge';
function show() {
console.log(company);
}
Explanation
Block scope, introduced with let and const, limits variable access to the block in which they are declared.
Code:
if (true) {
let age = 25;
console.log(age);
}