Learn Ternary Operators In 9 Minutes

preview_player
Показать описание
🚨 IMPORTANT:

Ternary operators are the source of lots of controversy in coding. Some people love them and others say they are a code smell. In this video I will break down when ternaries are good, when they are bad, and lastly how exactly you write a ternary operator.

📚 Materials/References:

🧠 Concepts Covered:

- What is a ternary operator
- When to use the ternary operator
- Why ternary operators are bad

🌎 Find Me Here:

#TernaryOperator #WDS #JavaScript
Рекомендации по теме
Комментарии
Автор

Yeah I've been telling Jr Devs on my team lately to "Be less clever, be more clear." Being clever just to collapse some expression down to 1 line is only good if that line is extremely readable. Otherwise, make it dumber and cleaner.

TylerR
Автор

Nice to see you going back to the roots of your channel with short yet comprehensive videos on a single concept. These are the types that initially drew me to WDS. Also, awesome thumbnail! 😄

nsharma
Автор

definitely agree on what this video covers. ternaries can be amazing, if you know when to use them. readability goes over "smart code", especially if you're working with others (or even if you have to look at your code a couple months later). it might not be obvious at first but readability saves you a ton of overhead in the long term.

BloodyScythe
Автор

Exactly what I feel about arrow functions too. I prefer “function” to be written out.

EmadElSammad
Автор

Superb.. You are the only one who make tutorial with explaining downside of it so we can be aware especially as new developers.

yousufkhan
Автор

SUCH A GREAT MENTOR!!!

I am learning JS for a year and now, I understand this from your lectures. Thank you, Kyle!

thedevcristian
Автор

4:54 Hmm, I think this is debatable. Personally I find this ternary use case quite simple to read & understand. And imagine if this 'if/else' block that you would use instead is nested inside another if, I believe it would be a lot more difficult to read than a simple ternary.

As for other comments in video I totally agree!

Keep up the great work and effort on making those really useful videos. :)

LukED
Автор

Readability is a subjective matter. As long as only one variable is to be set, nested ternary operators are no worse than not nested ones. That only depends on whether you are used to it or not.

fovyduw
Автор

Contrarian take: complicated ternaries can be easy to read, depending on how you handle linebreaks & indentation. For example, in a use case where stacked if/else if/else statements is often used, you can achieve something in JS close to what a when statement in Kotlin does:
const result = isTruthyCheck ? result1
: isTruthyCheck 2 ? result2
: result3

When statement example in Kotlin:
val objectType = when {
fileType === UnixFileType.L -> "l"
fileType2 === UnixFileType.HYPHEN_MINUS -> "-"
fileType3 === UnixFileType.D -> "d"
else -> "unknown file type"
}

A switch statement is often not possible in this context if the truthy checks don't inspect the same argument/variable. Plus, the need to explicitly break can be clunky at times. I often use this JS ternary approach and don't find it problematic. To each his own, though. Read-ability is important.

silencedogood
Автор

day 1 : building ai neural network and ml
day 2 : how to use ternary operator 😊

he would hv seen the comments and ben like these kiddos don't deserve shii

tilakmadichettitheappdeveloper
Автор

When I come across one in code I always have to take a second glance at it to understand what is going on (even when used properly). I don’t mind them but would prefer a standard if/else.

cyclox
Автор

I always require to put the ? and : on a new line for readability. And if you want to execute a function, you can restrict it to resolve which function to use, as this indicates that you do want to do something, but what is calculated. The only thing I agree on is never do nesting. All others are easier to read Imho, as you are more concise in what is intented.

DieterPrivate
Автор

Sir, you are doing great work by providing us not only skill and knowledge but indirectly you are creating job for people's like us whom have hungered to learn new things and using and work on it as professionally. Thanks once again your videos help me in my growing careers

lalit
Автор

I have been on the fence about ternary operator. While it does present cleaner. Depending on code base. It’s easy to miss the logic depending on the developers choice of formatting.

If / Else always stands out within the lines of code: but I also recognize the desire to eliminate additional code.

fragileglass
Автор

so i think because of the returning the value nature, ternaries operator can be used inside of return statement of any function, while we can't use if else there..

MrRawat-yddt
Автор

This is a really useful video, thank you. Some constructive feedback - highlighting text and wiggling the cursor back and forth quickly makes it very difficult to focus for some viewers

annam
Автор

this is superb, i have been trying to understand what my lecturer wrote for and hour now. but thanks once more... now i understand the downside of tanary operator very well

soyilastore
Автор

Not what I was looking for, but this is the best explanation I have run across for if:else shorthand! Merci beaucoup ~!

psilovechai
Автор

nested ternary can work with very good indentation :

console.log('z');

let x = 2;

let y = x < 0 ? "fii" :
x < 1 ? "fuu" :
x < 2 ? "foo" :
"faa" ;


console.log(y);

(but of course youtube will break the indentation).

Neckhawker
Автор

Please make a video on JavaScript Type Coercions clearly stating all the rules that JavaScript follows.

Wakkyguy