return Keyword
The return
statement exits the current function and optionally supplies a value to the caller.
Statements after return
in the same block do not run.
Syntax
return; // no value
return value; // with value
Examples
// ১) গ্রেড নির্ধারণ — early ফেরত
function গ্রেড(নম্বর) {
যদি (নম্বর < 0 || নম্বর > 100) {
কনসোল.ত্রুটি("অবৈধ নম্বর");
ফেরত; // value ছাড়া ফেরত
}
যদি (নম্বর >= 80) ফেরত "A+";
নাহলে যদি (নম্বর >= 70) ফেরত "A";
নাহলে ফেরত "A-এর নিচে";
}
// ২) প্রথম জোড় সংখ্যা খুঁজে ফেরত
function প্রথম_জোড়(অ্যারে) {
প্রতি (const n এর অ্যারে) {
যদি (n % 2 === 0) ফেরত n; // মিল পেলেই বেরিয়ে যাও
}
ফেরত null; // কিছু না পেলে
}
// ৩) অবজেক্ট/অ্যারে ফেরত
function পরিসংখ্যান(সংখ্যা) {
let মোট = 0;
প্রতি (const n এর সংখ্যা) মোট += n;
ফেরত { total: মোট, avg: মোট / সংখ্যা.length };
}
Notes
return;
without a value returnsundefined
.return
exits only the current function, not loops/blocks outside it.- Prefer early returns for validation to reduce nesting.
- When returning an object literal from a concise arrow function, wrap it in parentheses:
() => ({ ok: true })
. - In
try { ... } finally { ... }
, thefinally
block runs even if areturn
occurs intry
.