উদাহরণসহ TypeScript এর Conditional Ternary Operator

  TypeScript Conditional Ternary Operator with Example প্রিয় ডেভেলপারস ! আসসালামু আলাইকুম , আশা করি ভালো আছেন । TypeScript Operators সাধা...

 TypeScript Conditional Ternary Operator with Example

TypeScript Conditional Ternary Operator with Example

প্রিয় ডেভেলপারস! আসসালামু আলাইকুম, আশা করি ভালো আছেন TypeScript Operators সাধারণত JavaScript Operators এর মতোই কিন্তু TypeScript ডাটার স্ট্যাটিক টাইপ প্রদান করে থাকে যার ফলে কোডিং Error হওয়ার সম্ভাবনা অনেকাংশ কমে যায় নিম্নে TypeScript Operators এর Conditional Ternary Operator (কন্ডিশনাল / টার্নারি অপারেটর) উদাহরণসহ বর্ণনা করা হলো -

 

লক্ষণীয় যে, TypeScript Operators সম্পর্কে বিস্তারিত জানুন এখানে।

 

Conditional Ternary Operator (কন্ডিশনাল / টার্নারি অপারেটর)


? -  সাধারণত if-else এর সংক্ষিপ্ত ভার্সন যা এক লাইনের মধ্যে লেখা যায় সংক্ষেপে ternary operator বলেযদি কন্ডিশন সত্য হয়? তাহলে ভ্যালু এটা : না হলে ভ্যালু এটা

[

condition ? expressionIfTrue : expressionIfFalse;

]


[

// Example 0

let age: number = 20;

let message: string = age >= 18 ? "You are an adult" : "You are a minor";

console.log(message); // Output: "You are an adult"

]


[

// Example 1

let isRaining: boolean = true;

let weatherMessage: string = isRaining ? "Bring an umbrella" : "Enjoy the sunshine";

console.log(weatherMessage); // Output: Bring an umbrella

]


[

// Example 2

let temperature: number = 25;

let activity: string = temperature > 20 ? "Go for a walk" : "Stay indoors";

console.log(activity); // Output: Go for a walk


]


[

// Example 3

let isUserLoggedIn: boolean = true;

let username: string;

// Using the ternary operator to assign a value to username based on the login status

username = isUserLoggedIn ? "Admin" : "Guest";

console.log(`Welcome, ${username}!`);

]


[

// Example 4

function checkEvenOrOdd(number: number): string {

    return number % 2 === 0 ? "Even" : "Odd";

}

// Testing the function with different numbers

console.log(checkEvenOrOdd(8));  // Output: Even

console.log(checkEvenOrOdd(15)); // Output: Odd

]


[


// Example 5

function checkVotingEligibility(age: number): string {

    return age >= 18 ? "Eligible to vote" : "Not eligible to vote";

}


// Testing the function with different ages

console.log(checkVotingEligibility(25)); // Output: Eligible to vote

console.log(checkVotingEligibility(16)); // Output: Not eligible to vote

]

 

 

আশা করি আপনি আজকের এই ব্লগটি মনোযোগ সহকারে পড়েছেন আজকের ব্লগটি আপনার কাছে কেমন লেগেছে অবশ্যই  কমেন্ট করে জানাবেন, আর যদি কোন ভূল হয়ে থাকে তাহলে ক্ষমা সুন্দর দৃষ্টিতে দেখবেন এবং কোথায় ভূল হয়েছে কমেন্ট করে জানাবেনপোষ্টে উল্লিখিত কোনো অংশ যদি বুঝতে সমস্যা হয়, তাহলে কমেন্ট বক্সে জানিয়ে দিনইনশাআল্লাহ্‌, সমস্যা সমাধানের চেষ্টা করবো আর ওয়েবসাইটটি বন্ধুদের মাঝে শেয়ার করবেন আজকের মতই এখানেই বিদায় নিলাম, ইনশাআল্লাহ দেখা হবে অন্য কোন ব্লগে ভালো থাকবেন সুস্থ থাকবেন আল্লাহ হাফেয

COMMENTS

Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content