Logical Operators you need to know in JavaScript
Introduction
When we work with JavaScript, we deal a lot with conditions to make our platform dynamic, accommodate the needs of different user’s and ensure the platform is secure, hence understanding conditional logic is of utmost importance. Here are the different ways in which you can write conditional logic in JavaScript each way have its pros and cons, hence it is left for the developer to determine which parameters are of greater importance and in the best interest of their project. The pros and cons are mostly measured by their aesthetics, runtime, space and effective multiple logical implementations.
Before going through this article it is very important that you first of all, have a clear understanding of conditional logic in Javascript and I have linked some articles which gives some easy to grasp explanation. You can go through the article here: Definitive Guide to Conditional logic in JavaScript
You can also checkout the MDN details on logical operators here
Some conditional Logical Operators
- If/else
- Switch
- Ternary Operator
There’s more but let us stick to the above listed for now and to illustrate the listed in a function, we would be using the conditional logic below.
Let ‘s Position a set of students based on the range of their percentage score and award them a Prize accordingly
Position | Percentage | Award |
---|---|---|
1st | = 100% | 100k |
2nd | >= 90% | 90k |
3rd | >=80% | 80k |
4th | >=70% | 70k |
If/else Logical Conditional Operator
function studentPrize (position, per) {
if (!position || per << '70%') throw new Error('Student does not qualify for an award');
if ('per >= 70%'|| position == '4th') {
return `The ${position} positon, has been awarded 70K`;
}
if ('per >= 80%'|| position == '3rd') {
return `The ${position} positon, has been awarded 80K`;
}
if ('per >= 90%'|| position == '2nd') {
return `The ${position} positon, has been awarded 90K`;
}
Else ('per >= 70%'|| position == '1st') {
return `The ${position} positon, has been awarded 100K`;
}
}
Readup on if...else statement on MDN
Switch Logical Conditional Operator
function studentPrize (position, per) {
switch (position, per) {
case ('!position' || 'per << 70%')
:Output += 'Student does not qualify for an award'
Return;
case 'per >= 70%': case 'position == 4th'
:output += `The ${position} positon, has been awarded 70K`;
break;
case 'per >= 80%': case 'position == 3rd'
:output += `The ${position} positon, has been awarded 80K`;
Break;
case 'per >= 90%': case 'position == 2nd'
:output += `The ${position} positon, has been awarded 90K`;
Break;
case 'per == 100%': case 'position == 1st'
:output += `The ${position} positon, has been awarded 100K`;
Break;
Default: 'You have entered an invalid value';
}
}
Readup on switch statement on MDN
Ternary Logical Conditional Operator
studentPrize(position, per)
{ per >= 100% position == '1st' ? `The ${position} positon, has been awarded 100K`
: per >= 90% position == '2nd' ? `The ${position} positon, has been awarded 90K`
: per >= 80% position == '3rd' ? `The ${position} positon, has been awarded 80K`
: per >= 70% position == '4th' ? `The ${position} positon, has been awarded 70K`
: per >= 70% `Student does not qualify for an award`
return }
Readup on conditional (ternary) operator on MDN
Conclusion
Well, some Operators are not yet widely accepted and some argue which, is the best, based on which has better run time, easier to understand, widely accepted, have better aesthetics and so on, even more, don't forget browser compatibility. Well, I cannot outrightly pass a verdict on this. I believe everyone is entitled to their choices but it is also very important to state that if you are working with a team, to accommodate everyone one, ensure uniformity and to meet the requirements of your clients, ensure you cooperate with your team and client irrespective of your personal preference except if of course, they are flexible and open to different Operatorss. I would like to believe we all know the most widely accepted and used operator for a logical condition or a function in javascript is if/else.
You would notice that even within each operator there are different implementation method aside the illustrations above and that is because, JavaScript is a broad and flexible language it is accommodating of different thought process and implementation styles, pitch your tent and always ensure you have a good grip of one method before moving onto the next ‘just like learning different programming languages’ familiarise with different methods of course ‘you never can tell’ but build on your strength to become one of the best of the best or should I say one of the 1.1% as recruiters popularly say. Moreover when you are well grounded at one it becomes easier to adopt any other area of interest.
‘It is better to be a jack of one trade and a master of it than to be a jack of all trade and master of none’ The fact that you are a jack of one trade does not mean you are a complete novice at the rest, it only means you are best at your choice and the kingpin in your pitch.
Further reading
Read on and state other conditional logic s you are familiar with
Read on and comment some libraries with syntaxes that support conditional logic you may have used or know