Automatically translated.View original post

Logical / JavaScript Operator

# Web Site Writing Basics

# javascrip

# The logical operator

Logical Operators in JavaScript use Boolean: true or false) or to control the operation of conditions. They work on a principle called Short-Circuit Evaluation. This means that they may stop evaluating the remaining Operand as soon as the final result is known.

There are 3 main operators:

1. (OR: OR)

The OR operator is used to determine if a condition is true.

Truth table: It returns true if one or both operands are true.

Short-Circuit Operation:

It will evaluate the operand from left to right.

As soon as the first operand is found to be Truthy (considered true), it restores the value of that operand and stops evaluating.

If all operands were Falsy (considered false), it would restore the value of the last operand.

Example Results Reason

'True false. '

'0 5 '

'"null'

General benefit: Used to define Default Value if the first value is null or undefined.

JavaScript

let userName = userSetting | | 'Guest';

/ / If userSetting is null / undefined / "/ 0, userName is set to 'Guest'.

2. & & (AND: AND)

The AND operator is used to determine if all conditions are true.

Truth table: It returns true only if both sides of the operand are true.

Short-Circuit Operation:

It will evaluate the operand from left to right.

As soon as the first operand is found to be Falsy (considered false), it restores the value of that operand and stops evaluating.

If all operands were Truthy, it would restore the value of the last one.

Example Results Reason

The first true & & false false is true, but the second is false, thus restoring false (the second).

5 & & "Hello" "Hello" Both Are Truthy Restore Last

0 & & "World" 0 0 is Falsy immediately returns 0.

Typical Benefits: Used to Make Conditional Rendering or Call Conditional Functions (Used instead of Short If Structures)

JavaScript

/ / If isLogged is true, call the showProfile function.

isLogged & showProfile ();

3.! (NOT: NOT)

The NOT operator is used to reverse the logical value.

Operation: It will convert the operand into a logical value (Boolean) and then reverse it.

! True, false.

! False is true.

Example Results Reason

! True, false. Back up.

! 0 true 0 to Falsy was converted to false and then returned to true.

! "Hello" is true! The first converts "Hello" (Truthy) to false and the second back to true.

Export to sheet

General benefit: Used to verify whether a value is Falsy or Truthy (using!! to convert any value into an authentic Boolean).

Falsy and Truthy Values in JavaScript

In JavaScript logic valuation, the following values are assumed to be Falsy (equivalent to false):

False

0 (zero)

"" (empty string)

Null

Undefined

NaN (Not-a-Number)

Truthy is all other values that are not in the Falsy list (e.g. 1, "hello," [], {}).

2025/10/7 Edited to

... Read moreในบทความนี้เราจะเพิ่มเติมความเข้าใจเกี่ยวกับตัวดำเนินการทางตรรกศาสตร์ (Logical Operators) ใน JavaScript ที่แม้จะเป็นเรื่องพื้นฐานแต่ก็สำคัญมากในการเขียนโปรแกรมที่มีเงื่อนไขซับซ้อน นอกจากนี้ยังมีประเด็นที่หลายคนอาจสงสัยเกี่ยวกับข้อมูลชนิดข้อความ (string) กับการใช้งานตัวดำเนินการเหล่านี้ สิ่งที่อยากแชร์ก็คือการทำงานของตัวดำเนินการ OR (||) และ AND (&&) นอกจากจะช่วยให้เรากำหนดค่าตัวแปรเริ่มต้นได้แบบรวบรัดแล้ว ยังช่วยในกรณีของ Conditional Rendering หรือการเลือกทำงานในบางสถานการณ์อย่างมีประสิทธิภาพ เช่น ถ้ามีค่าของตัวแปรเป็น null หรือ undefined เราสามารถใช้ || กำหนดค่าเป็นค่า default ได้โดยไม่ต้องเขียนโค้ดยาวๆ ส่วนตัวดำเนินการ NOT (!) ก็มีประโยชน์มากในการตรวจสอบว่าค่าตัวไหนเป็น Falsy หรือ Truthy จึงช่วยลดความซับซ้อนในการเขียนเงื่อนไขและทำให้โค้ดอ่านง่ายขึ้นอย่างมาก นอกจากนี้ การเข้าใจเรื่อง Short-Circuit Evaluation ก็สำคัญไม่น้อย เพราะช่วยให้โค้ดที่เราเขียนทำงานได้รวดเร็วและไม่ต้องเสียเวลาในการตรวจสอบเงื่อนไขที่ไม่จำเป็น ในการใช้งานจริง ตัวอย่างเช่น การตรวจสอบสภาวะการล็อกอินผู้ใช้ ถ้าตัวแปร isLogged เป็น true ก็เพียงพอให้เรียกฟังก์ชันแสดงข้อมูลโปรไฟล์ได้ทันที โดยไม่ต้องใช้ if ซ้อนหลายชั้น ซึ่งช่วยให้โค้ดดูสะอาดและเข้าใจง่ายมากขึ้น สำหรับการประเมินค่าตรรกะใน JavaScript นั้น เราควรจดจำค่าที่ถูกมองว่าเป็น Falsy ซึ่งรวมถึง false, 0, "" (สตริงว่าง), null, undefined และ NaN ส่วนค่าที่เหลือจะถือเป็น Truthy ทั้งหมด ซึ่งความรู้ตรงนี้จะช่วยให้เราเขียนเงื่อนไขต่างๆ ได้อย่างถูกต้องและลดข้อผิดพลาด แนะนำว่าควรลองนำตัวดำเนินการเหล่านี้ไปใช้ในโปรเจกต์เล็กๆ หรือในโค้ดที่เขียนเองดู เพื่อเรียนรู้และเข้าใจพฤติกรรมของมันอย่างแท้จริง เพราะประสบการณ์ตรงจะช่วยให้เราเข้าใจแนวคิดและการนำไปใช้ได้ดีกว่าการอ่านอย่างเดียวแน่นอน

Related posts

Doubting yourself is no longer logical, it’s emotional
It doesn’t make sense to keep doubting yourself. You have so much evidence showing you that you are capable of solving tough problems. You’ve done so much m, you’ve grown so much, and come so far. Doubting yourself is no longer logical, it’s emotional. And probably a habit. You are ready for whatev
Fearlessly Resilient

Fearlessly Resilient

30 likes

Feeling less logical when you are stressed ?
I am an Ambassador for an organization of doctors. Follow for more. #nervoussystemregulation #fightorflight #somatichealing #somatic
howtobehueman

howtobehueman

11 likes

What were they thinking?? 🫣 | @TheTyee
Rachel Gilmore

Rachel Gilmore

31 likes

An image titled "SMELLS - Memory Crossing Time" features burning incense, a lit candle, lavender, and essential oil bottles. The text explains how scents are powerful spiritual messengers, bypassing logic to connect with memory and emotion, and lists common spiritual scent experiences and their meanings.
Spiritual smells
Sometimes Spirit doesn’t speak in words. It drifts in on a scent. A flower with no vase. Coffee when no one’s brewing. Perfume that belongs to someone you loved. Smell travels where time can’t interfere. It bypasses logic and goes straight to memory, emotion, and knowing. If a scent ap
Hug life 🌸

Hug life 🌸

9 likes

Phallological Museum
The Phallological museum! Iykyk! #phallusmuseum #phallologicalmuseum #terrastravels #terrakimberlyscott #Iceland
TerrasTravels

TerrasTravels

282 likes

Crying releases oxytocin and endorphins aka your brain’s natural sedatives. Bottling it up doesn’t make you strong. It just makes your nervous system tired and petty. Fun fact: For trauma-wired and ADHD brains, crying isn’t emotional chaos ✨ it’s regulation. Letting yourself cry to completion he
Katarina Scott ✨ ECA Tea 🫖

Katarina Scott ✨ ECA Tea 🫖

1 like

By the way, part of that antisemitism is suggesting that Jewish people should be and our only safe in a fascist ethnic state, but consequently they will also blame Israel for everything. It does not have to be logical. It just has to be helpful.
Will McC

Will McC

1 like

This image displays verb conjugations for 'have' across various tenses, including Present Simple, Past Simple, Future Simple, Present Continuous, Past Continuous, Present Perfect, Past Perfect, Future Perfect, and Present Perfect Continuous, for different pronouns.
This image lists common medical abbreviations such as ICU, BP, HR, BMI, Dx, Tx, Sx, Rx, Hx, Px, FU, BID, TID, QID, and OD, along with their full meanings.
This image provides a comprehensive list of time vocabulary, including units of time (e.g., 24 hours = 1 day), periods of the day, and various time-related concepts like 'deadline' and 'schedule'.
ለምን?

ለምን?

249 likes

Your 3-Day Journey to Self-Compassion 🌷
Imagine if you spoke to yourself like someone you deeply care about. 🌸 The Self-Compassion Journal makes that possible in just 3 days. Here’s how it works: 🧠 Left Brain Prompts – Logical, thought-provoking questions to help you understand yourself. 💖 Right Brain Reflections – Gentle exercis
Mind Reflection Method

Mind Reflection Method

2 likes

I wish I didn’t have to work today #fypシ #raptureready #grwm #fastfoodlife #student
Annaygonzalezz

Annaygonzalezz

1 like

#citypharma en #paris 📍 26 Rue du Four, 75006 Paris, France #skincare #pharmacy
Emma Duarte

Emma Duarte

3 likes

Pt1 #viral #fypシ゚viral #fypシ #fypage #dating
Yagirlgabby

Yagirlgabby

62 likes

Right before you get a love offer.♥️ #etherealkra
Kra'

Kra'

5 likes

The Logical Song
Captivating Logic, Timeless Melody. #classicpop #vintage #timeless #lemon8
Milagros

Milagros

191 likes

The image defines the "no true Scotsman" logical fallacy, explaining it as an appeal to purity to dismiss criticism by shifting goalposts. It includes an example about Scotsmen and porridge, and features an icon of a creature in water.
No Tue Scotsman Logical Fallacy
Alot of Lefties suffer from this textbook logical fallacy about protesting in Portland. Which has led to the inevitable end of the BLM, Roe v Wade, Gaza at PSU & now the current movement. This is NOT "the will of the community." This is a tactic used to divide the community, make everyo
Channel Heed

Channel Heed

2 likes

#3 gets most believers without them even noticing
1. Distraction – He’ll keep you busy, unfocused, or always “too tired” to pray or read the Word. 2. Deception – Twisting truth to make lies sound logical (just like in Eden). 3. Division – Causing offense, comparison, gossip, and relational strife to break unity. 4.
𝔭𝔬𝔴𝔡𝔢𝔯’𝔰 𝔭𝔯𝔞𝔶𝔢𝔯⨾༊

𝔭𝔬𝔴𝔡𝔢𝔯’𝔰 𝔭𝔯𝔞𝔶𝔢𝔯⨾༊

227 likes

The image defines the 'Appeal to Common Sense' logical fallacy, describing it as asserting conclusions are 'common sense' without justification. It includes the logical form: 'It's common sense that X is true. Therefore, X is true.'
The image defines the 'Appeal to Common Belief' (argumentum ad populum) logical fallacy, explaining it as using the widespread acceptance of a belief as evidence for its truth, without requiring actual proof.
The image defines the 'Avoiding the Issue' logical fallacy, describing it as responding to an argument by not addressing its points, simply sidestepping the argument. It provides the logical form: 'Person 1 makes claim X. Person 2 makes unrelated statement.'
common logical fallacies
The antiscience community heavily depend on a core group of logical fallacies. #logicalfallacies #science #educational
Allie That RN

Allie That RN

7 likes

Pick a Member of BTS for a Tarot Reading♥️
It’s a BTS pick a card reading! Pick your bias, your bias wrecker, or whatever you feel drawn to for for a tarot reading. 🔮 Remember to take whatever resonates and leave the rest! #tarot #BTS #btsarmy #kpop #tarotreadings
Archer.Arcana

Archer.Arcana

541 likes

Logical
#theguy #comedyskit #funnyvideos #randomhumor #jokes
The Guy

The Guy

15 likes

Logical fallacy
#minneapolis #ice
Gal Fieri

Gal Fieri

3 likes

🏊‍♀️ Can you swim after an embryo transfer? Dr. Dan shares what’s safe, what’s not, and why timing matters when you’re in the 2-week wait. #embryotransfer #ttccommunity #ivf #ttc
Bird&Be

Bird&Be

1 like

B din strategical & logical not emotional & blinded ☮️🕉️⛎☯️🐍🦅🐯
Jatos211⛎🕉️☯️☮️

Jatos211⛎🕉️☯️☮️

0 likes

#hypermobility #heds #pots #dysautonomia #hsd
getwell.withnell

getwell.withnell

0 likes

Only 1 logical explanation..
#comedy #funny #Lemon8Diary
Riley Roman

Riley Roman

3 likes

It's a logical conclusion 😁
#bookshelf #bookish #organization #tbr #bookrecs
🍀Sarah🍀

🍀Sarah🍀

3 likes

Sitting by my worst fear
—the other one. Story time ©️deardaisy Instead of the tear on my heart I was solely focused on this sister bee that surely would have stung me. They were there first after all. Not everyone was allergic to bees, wasps and such but no one wanted to find out in that one fateful moment. It
Ada

Ada

7 likes

🍋 Horror/Physicological Thriller 🍋
⭐️⭐️⭐️ I liked this book a lot. I honestly want to find the book “How to be a Groovy Witch” One thing I didn’t like is how descriptive it was when Fern went to the hospital and was in labor and it all of a sudden i was reading doctor’s language. I did enjoy the book of how they casted a spell on
Brandy

Brandy

12 likes

Words vs. Violence: Why Americans Are Awakening #dialogue #violence #speech #politics #freedomofspeech
Day2DayClips

Day2DayClips

4 likes

See more