Truthy/Falsy চেক
নিচের ফলাফল পেলে আপনি বুঝতে পারবেন ভেরিয়েবল null নাকি undefined। যদি ফলাফল false হয়, তবে ভেরিয়েবল null এবং undefined।
উদাহরণ: Truthy ভেরিয়েবল
<html>
<body>
<script>
var age = 10;
if (age) {
document.write("True");
} else {
document.write("False");
}
</script>
</body>
</html>
typeof দিয়ে অস্তিত্ব চেক
<html>
<body>
<script>
var age = 10;
if (typeof age !== 'undefined') {
document.write("True");
} else {
document.write("False");
}
</script>
</body>
</html>
সম্পূর্ণ চেক ফাংশন
function isEmpty(value) {
// null, undefined, "", 0, false, NaN
return value == null || value === "" || value === 0 || value === false || Number.isNaN(value);
}
// বা শুধু null/undefined/empty string
function isNullOrUndefinedOrEmpty(value) {
return value === null || value === undefined || value === "";
}
// ব্যবহার
console.log(isEmpty("")); // true
console.log(isEmpty(null)); // true
console.log(isEmpty(undefined)); // true
console.log(isEmpty(0)); // true
console.log(isEmpty("hello")); // false
Falsy মান들의 তালিকা
false0,-0,0n(BigInt)"",'',``(খালি স্ট্রিং)nullundefinedNaN