개를 받습니다(다른 연산자는 모두 피연산자 한 개 또는 두 개를 받습니다). 조건 연산자는
if
...
else
문과 동등한 표현식입니다. 다음 예제를 보십시오.
const
doIt
=
false
;
const
result
=
doIt
? "
Did
it
!" : "
Didn
'
t
do
it
.";
물음표 앞에 있는 첫 번째 피연산자(
doIt
)가 참 같은 값이면, 이 표현식의 값은 두 번째 피연
산자(물음표와 콜론 사이)이며, 거짓 같은 값이면 이 표현식의 값은 세 번째 피연산자(콜론 다
음)입니다. 초보 프로그래머들은
3
항 연산자를
if
...
else
문을 혼란스럽게 바꿔 놓은 것 아니
냐는 식으로 생각하지만,
3
항 연산자는 문이 아니라 표현식이므로 다른 표현식과 결합하여 매
우 유용하게 사용할 수 있습니다.
5.8.4
쉼표 연산자
쉼표 연산자는 표현식을 결합하여 두 표현식을 평가한 후, 두 번째 표현식의 결과를 반환합니
다. 표현식을 하나 이상 실행해야 하지만 값으로 필요한 것은 마지막 표현식의 결과뿐일 때 쉼
표 연산자를
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.