简体   繁体   中英

Can someone explain me why we use parentheses around conditions in javaScript

for an example if we take the below code Ex:

if (5 > 4) {}

in the above code I need to know why we use parentheses around the if statement's condition we can just use the below code like

if 5 > 4 {}

but why do we include parentheses around the if statement's condition for what purpose do we use it is it just to separate the condition and the {} block of the if statement or what

You use parentheses around the condition because you must. The syntax of javascript demands that for if statements, the condition is wrapped in parentheses. Leaving them out is a syntax error.

If you're asking why the language was designed to have that particular syntax instead of some other, none of us were in the room when that decision was made. So about the best we can offer you is the answer 0.sh gave in the comments: "because Brendan Eich said so". Brendan Eich was the original creator of the javascript, and so he laid the foundations of its syntax. Whatever his motivations were for this decision, that's the reason it is that way.

It's possible to design languages so that the parentheses are optional, but that's just not the way javascript was designed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM