|
|
|
|
|
by ggreer
4010 days ago
|
|
Some style preferences are subjective, but some have very good reasons for being the way they are. Here's a simple JavaScript function that returns an object... function blah()
{
return
{
key: "value"
};
}
...except it returns undefined when invoked: console.log(blah());
undefined
Can you spot the bug? With so little code, it should be obvious, right? Before reading on, stop for a minute and really try to find the error.... Figured it out? ... The answer is that JavaScript has automatic semicolon insertion. That means there's effectively a semicolon on the same line as return. ASI is why, in JavaScript, you always put the curly brace on the same line. Sure, you could try to remember the ASI rules, but you're guaranteed to be safe if you just put your braces on the same line. And considering how much code a typical programmer writes, you are almost guaranteed to inflict an ASI bug on yourself if you don't do this. |
|