JavaScript tip: If and Else Shorthand | #shorts

preview_player
Показать описание
Hey everyone 😀 As a programmer, you should be able to write short, clean and understandable codes! In this tutorial, I show you how you can make If and Else conditions shorter. Don't forget to subscribe me for more awesome tutorials 👇

🔔 Subscribe Now!

🎵 Music
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Track: BEAUZ & JVNA - Crazy [NCS Release]
Music provided by NoCopyrightSounds.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Thanks for watching, I appreciate it 🙏
Made with 💗 by Hossein
Рекомендации по теме
Комментарии
Автор

Don’t over complicate your code or you won’t be able to understand it when debugging fam js

aesap_
Автор

const message = “Hey “ + (gender == “girl” ? “girl” : “boy”)

No need to repeat the “Hey” twice

travisislit
Автор

Congratulations on discovering the ternary operator.

theomegamale
Автор

It's called ternary operator.
You can also use ?: (In php) If you only want else on one line (elvis operator), or ?? If you want to go true if null.

yohan
Автор

Better and more debugable way I would prefer:

let msg = "Hey Boy";

if (gender == "female") {
msg = "Hey Girl";
}

Apart from being clean and more easily debugable, one another advantage is that you can add more lines of code easily inside the "if" block when required.

redstarentertainment
Автор

let gender = "girl"

Ur prog: HEI BOIIII

thegate
Автор

HUGE advantage most people seem to missing about this approach: Look at the scope of the constant (const).

If you define a constant inside an if/else statement, you cannot access it outside the statement (block scope), which is exactly what you need most of the time. This leads to many people using “let” to define the variable outside of the if/else and then assigning the value inside the statement - which is both messy and less performant when used for a value which is not going to change (ie. a constant).

By using the ternary operator, you can define a constant at the correct block scope and use it within any future code, avoiding incorrect use of “let”.

trimonmusic
Автор

ternary op is good for if else but if you use it for nested condition then it make readability worse and debugging too

js-dev
Автор

Beginners also add unnecessary spaces inside nested objects. You might not want to add extra spaces when you don’t need to, or in the future you’ll have a huge refactoring problem.

mohitsrinivasan
Автор

Thx for the clip. Ternary operators make your life easier sometimes, but here are some thoughts about the clip:
1) the gender example is a poor one (more than 2 options) - a switch statement would suit you better in that case
2) ternary expressions do not increase the readability and maintanability of your code. Especially when you have more than two options (as you would have in that case)
3) to make you example for „professional“ coding complete i am missing the early return - that is an essential

hansheinrich
Автор

as someone learning. i should stick with the classic way for now. i'll definitely use shortcuts going forward if it calls for it.

tukos
Автор

Ternary expressions return a value, if statements do not. I haven't written an if/else statement in a long time. Try to keep all if statements as single line guards (where possible)

brennenherbruck
Автор

Uhh, we use strict equality operator ===

donaldjr.labajo
Автор

This is such a unprofessional vid, it’s so cursed

vsmnigw
Автор

Great for when it’s just a simple if else statement but gets messy if there are more else statements involved. Crucial keu

devinbridgelall
Автор

I think clarity is better than conciseness, its shorter but many people dont know it, just dont do it in your workplace or in projects, but hey thats a cool trick and thanks for the video

chococereal
Автор

The ternary operator! Shortens the oblong nature of If Else Statements specifically!

Although not a replacement, when size and loading speed matters- and you know that you won’t have to go back to fix it, the ternary operator can help!

TheRealAnsontp
Автор

I have never seen a single "pro" use a turnery operator rather than an if/else block rather than in tutorials. They are just hard to read and an unnecessary syntax barrier. I believe the switch-case/when statements are wayyyy more efficient

rarox
Автор

yes this type of coding if else is great for small checks

yt-spikegaming
Автор

You could probably take a bit further by using string interpolation
E.g.
Const message =`Hey ${rest of ternary statement}`

Oribi