No video

Logical operators && TRICKS with short-circuit evaluation - Beau teaches JavaScript

  Рет қаралды 71,022

freeCodeCamp.org

freeCodeCamp.org

Күн бұрын

Short-circuit evaluation happens when using conditionals with && or ||. The second argument is only evaluated when the first argument is not enough to determine the value of the expression. Learn some tricks using short-circuit evaluation in JavaScript.
Code:
🔗 codepen.io/beau...
More info:
🔗 www.jstips.co/e...
Beau Carnes on Twitter: / carnesbeau
⭐JavaScript Playlists⭐
▶Data Structures and Algorithms: • Data Structures and Al...
▶JavaScript Basics: • JavaScript Basics Course
▶Design Patterns: • Design Patterns - Beau...
▶ES6: • ES6 - Beau teaches Jav...
▶Clean Code: • Clean Code - Beau teac...
-
We're busy people who learn to code, then practice by building projects for nonprofits. Learn Full-stack JavaScript, build a portfolio, and get great references with our open source community.
Join our community at freecodecamp.com
Read great tech articles at medium.freecod...

Пікірлер: 40
@vitaliypogoretsky3199
@vitaliypogoretsky3199 6 жыл бұрын
|| stops on "truth", && stops on "lie". Remember :)
@firasalsghier3944
@firasalsghier3944 4 жыл бұрын
Thank you you really helped me
@nahidestes
@nahidestes 3 жыл бұрын
well said👏
@poonamthorup2458
@poonamthorup2458 3 жыл бұрын
Comment saved in my notes
@CambiaAhora
@CambiaAhora 3 жыл бұрын
awesome
@cheribrandt8918
@cheribrandt8918 2 жыл бұрын
feras alsghier Yea
@bruhmoment3731
@bruhmoment3731 2 жыл бұрын
Thank you!
@shaikhzoyeb
@shaikhzoyeb 6 жыл бұрын
thanks that's helpful
@muthimuthi5087
@muthimuthi5087 4 жыл бұрын
S qx!x fastest-growing yyxyyyyy try 6dyes ytx city 6dyes d try zzz dyxyxyyxyyxy Urdu and dydydy strategy dyes Rudy x US ydy sags y u uddx 6dye, xyxyxyxxy66xd x dxxdd b h ddxuxuddx hybrid fu d 7dzd h 877xu87xu7877xu87xu7yxyxmmmmmmmmmmm0?+ mhmm mmyxyx TRY RUDY
@MarcoAntonio-fh7rd
@MarcoAntonio-fh7rd 7 жыл бұрын
I was searching for the last explanation, thanks Beau, great tutorial
@busyrand
@busyrand 2 жыл бұрын
React sent me....
@aybak3k
@aybak3k Жыл бұрын
cam here to learn it figured out i do it all the time in bash 😆
@momakplayz7985
@momakplayz7985 3 жыл бұрын
What about "!" statement?
@stomperhk9107
@stomperhk9107 2 жыл бұрын
It reverses the value. E.g: !true // false !false // true !0 // true
@SogMosee
@SogMosee 6 жыл бұрын
Is it possible to use short-circuit evaluation with multiple variables? I would think so, but for some reason, this fails: let amountLength = transactionAmountField.value.length === 0, methodLength = methodSelect.value.length === 0, categoryLength = categorySelect.value.length === 0, disable = () => transactionSubmitButton.disabled = true, activate = () => transactionSubmitButton.disabled = false (amountLength || methodLength) && disable() The disable function is not called despite both amountLength and methodLength being equal to 0.
@SogMosee
@SogMosee 6 жыл бұрын
Nevermind, I figured it out! Here is the correct code: !!(amountLength || methodLength) && disable() !!(!amountLength && !methodLength) && activate() The first variable merely needed to be converted into a boolean.
@andrewmiddleton2308
@andrewmiddleton2308 2 жыл бұрын
@@SogMosee What`s the !! for?
@fwostedfwakes733
@fwostedfwakes733 4 жыл бұрын
I'm confused at the 3:40 example. Shouldn't !test run isTrue() instead of isFalse() because !test is actually true?
@heyman3731
@heyman3731 3 жыл бұрын
i hope you have found the solutions or it . but for anyone wondering or confused about that part of code :. if (!test) : means that if test is not true . this is the condition we gave to to js . beau changed the value of test to FALSE . Which resulted in javascript checking if the test is not equal to True , which eventually turn out to be right thats why the block code was executed .
@Moist_yet_Crispy
@Moist_yet_Crispy 5 жыл бұрын
Great video! Thanks for this!
@christianrodier3381
@christianrodier3381 6 жыл бұрын
That was very helpful thanks
@keifer7813
@keifer7813 2 жыл бұрын
4:49 How can you call a function with no arguments when the function declaration has a parameter?
@shilpas5441
@shilpas5441 2 жыл бұрын
Apparently the browser assigns the parameter with 'undefined'. So the function call is still valid.
@eugenenovikov671
@eugenenovikov671 4 жыл бұрын
function(name) { name = name } - are u serious?????????????????????
@elhadiziat764
@elhadiziat764 Жыл бұрын
very confusing explanation with very quick code writing!
@CarlosEstebanLopezJaramillo
@CarlosEstebanLopezJaramillo 7 жыл бұрын
You know what's lacking? to explain the caution when using short circuit evaluation when assigning a boolean, what if you expect a boolean but you just want to avoid having null or undefined?
@stomperhk9107
@stomperhk9107 2 жыл бұрын
Use the double not (!!) operator or the double question mark (??) operator, I didn't undestand wgat you tried to say.
@CarlosEstebanLopezJaramillo
@CarlosEstebanLopezJaramillo 2 жыл бұрын
@@stomperhk9107 Can't remember what I was going for, probably exactly that (??) operator, which I think did not exist or was not yet implemented 4 years ago when I made the comment.
@stomperhk9107
@stomperhk9107 2 жыл бұрын
@@CarlosEstebanLopezJaramillo Uhum, I see. I think that it didn't existed 4 years ago either.
@CarlosEstebanLopezJaramillo
@CarlosEstebanLopezJaramillo 2 жыл бұрын
@@pixelyd7729 Don't have a reason to change it.
@keifer7813
@keifer7813 2 жыл бұрын
@@CarlosEstebanLopezJaramillo I wouldn't remember either after 5 years haha
@GretSeat
@GretSeat 2 жыл бұрын
I'm even more confused now.
@emilybrock3822
@emilybrock3822 Жыл бұрын
Yife
@marianenkoulas4953
@marianenkoulas4953 Жыл бұрын
Gbvgg
@roguehoe
@roguehoe 2 жыл бұрын
Uh
Logical Short-Circuiting in JavaScript
9:51
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 6 М.
Coding Was HARD Until I Learned These 5 Things...
8:34
Elsa Scola
Рет қаралды 341 М.
WILL IT BURST?
00:31
Natan por Aí
Рет қаралды 40 МЛН
Functions - Beau teaches JavaScript
7:18
freeCodeCamp.org
Рет қаралды 69 М.
Copying Arrays (deep and shallow) - Beau teaches JavaScript
5:56
freeCodeCamp.org
Рет қаралды 45 М.
Short Circuiting With && and || (JavaScript)
4:09
ByteGrad
Рет қаралды 3,8 М.
#11 Logical Operators in Java
11:17
Telusko
Рет қаралды 137 М.
for in / for of - Beau teaches JavaScript
4:32
freeCodeCamp.org
Рет қаралды 77 М.
5 JavaScript Concepts You HAVE TO KNOW
9:38
James Q Quick
Рет қаралды 1,4 МЛН
Build this JS calculator in 15 minutes! 🖩
15:20
Bro Code
Рет қаралды 524 М.
C Programming Tutorial 54 - Short Circuit Evaluation
8:05
Caleb Curry
Рет қаралды 13 М.
WILL IT BURST?
00:31
Natan por Aí
Рет қаралды 40 МЛН