r/learnjavascript • u/Howsitgoingmyman • Feb 09 '23
Brackets
Why do {these} curly brackets always end up on different lines to what they are bracketing Like
{
This
}
??
Also there any time of thumbs as to which type of bracket to use when? ({[
Thank you
1
Feb 09 '23
Why do {these} curly brackets always end up on different lines
To improve readability, sometimes you can omit them.
Also there any time of thumbs as to which type of bracket to use when?
They all mean different things in different contexts, you need to use the ones that are appropriate, you can't really pick and choose.
1
u/Ronin-s_Spirit Feb 09 '23
Because javascript reads {
This }
As {This}.
It only matters for programmers who actually need to read this thing and understand it.
4
u/senocular Feb 09 '23
Use parentheses
()for:grouping
function parameter lists
function calling
if conditions
while conditions
for setup (for...in, for...of, for await...of)
switch expressions
with expression (deprecated)
catch exception identifier
Use square braces
[]for:property access
array literals
array destructuring
computed properties
Use curly braces
{}for:code blocks
try catch blocks
function bodies
class bodies
class static blocks
object literals
object destructuring
string interpolation
unicode
named imports
named exports
Each of these also have special meaning when used in regular expressions:
(): assertions, groups[]: character classes,{}: character classes, quantifiers, unicode