Web Tools #543 - JS Grouping, React Tools, Git/CLI, VS Code Tools
source link: https://mailchi.mp/webtoolsweekly/web-tools-543
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
JS Grouping, React Tools, Git/CLI, VS Code Tools
|
Issue #543 • December 14, 2023
Advertisement
|
Build with the Power of Code — Without Writing Any Take control of HTML, CSS, and JavaScript in a visual canvas. Webflow generates clean, semantic code that’s ready to publish or hand to developers. |
|
The one operator that we rarely think much about in JavaScript is the grouping operator. This is a reference to the use of parentheses, which come in handy in a number of circumstances in JavaScript to make sure code evaluates the way we expect.
Besides the use of the parentheses for the console.log() calls, you'll notice how the use of the grouping operator (i.e. the parentheses around "a + b") changes the result of the expressions on the last two lines. In the first log, the multiplication gets evaluated first, providing a final value of 7. But in the next log, I've grouped the two variables added together, so they get evaluated before the multiplication occurs, which changes the result.
The above is interpreted as the start of a function declaration, so it won't work. To get around this, you can use the grouping operator:
Notice the parentheses placed around the section starting with the function keyword up until the end of the curly braces. Again, nothing too groundbreaking here, and you've likely used something similar to this structure before. But maybe you didn't think specifically that this is happening due to the use of parentheses as a grouping operator.
As you can see, no curly braces, no return keyword. Yet the evaluated expression is returned automatically. This is a characteristic of arrow functions.
The above doesn't work as expected because the beginning of the object literal (the opening curly brace) is interpreted as the start of the function body for the arrow function (you can use curly braces for arrow functions).
The simple addition of the parentheses helps the code to be parsed correctly and now the object literal is returned as expected. |
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK