Expressions vs statements in JavaScript, know the difference
ฝัง
- เผยแพร่เมื่อ 18 ต.ค. 2024
- Understanding the difference between expressions and statements, and knowing how to convert statements into expressions is really important for success in frameworks built on top of of JavaScript that use JSX.
ℹ️ This video is part of a crash course series, JS Essentials for WP Devs. Get the entire series, the code repository, and more by signing up for the series via email: aurooba.com/js...
If you found this video helpful, please subscribe and hit the like button!
I knew ternaries, but this explanation of _why_ you need to use them in JSX is really clarifying! They are expressions! Thanks, Aurooba!
My pleasure! It's one of those questions that just bugs you in the back of your mind right?
Great explanation, Aurooba. Excited for more videos!
Thanks Nazim! Let me know if you have any questions as more videos come out. Happy to answer things and clarify as we go!
Great lesson & explanation! 👏🏼
Glad you liked it!
keep it up Aurooba, Thanks for sharing. which theme you are using ?
Glad you enjoyed the first lesson in the series! Do you mean my VS Code theme? I used Dracula Pro. 😊
Great content!
Glad you enjoyed it! Looking forward to your feedback on the rest as it comes out!
In my opinion, this is one of the really nice things about Svelte’s template syntax over JSX: statements are allowed 😍
I think in a well architected React component, you won't see too many expressions. In my opinion what makes the return area of a JSX component highly readable and usable is when the logic has been abstracted away BEFORE the return statement, leaving you with very few expressions and just other React elements. So from that perspective, I don't really mind. But I totally get what you mean.
🤔 I get an expression, I think, but not a statement. Is a statement an expression plus something else? Does it have to contain an expression?
Ah. The video focusses more on expressions because it's the expressions you need to definitely understand. However, this is a really good question Mark! A statement is one complete instruction in programming. There are simple statements and compound statements that have other statements nested inside them.
There are two main kinds of statements: declaration statements that set or assign something (like say a statement that assigns a value to the variable 'label') and control statements (sometimes called control flow statememts) which dictate the order of how other statements should be conducted (think an if statement or a while loop or a for loop).
So if I were to put it in more plain english language, a statement is a complete instruction with a subject and a verb. For example a "return statement", there the verb is "return" and the subject is whatever we are returning.
Does that help clarify things a bit more?
Yes that is clearer now - thanks