Javascript-styleguide

Opera Software Javascript Styleguide

Opera Software JavaScript Styleguide

In general, we are using the Google JavaScript Styleguide, but on top of that we have defined some rules that clarify things not stated there.

As working on JavaScript involved a great deal of working with HTML and CSS, please also check out the Google HTML/CSS Styleguide and follow it.

Use strict comparision

Type–converting comparison (like ==) can have suprising results. You are encouraged to avoid using them and use strict comparision (like === and !==) instead.

Promises: Always use a rejection handler

Code inside promises that fails will not throw to the console. This can lead to bugs going undetected, so always add a rejection handler.

myPromise().then(doSomething).catch(function(e) { throw e; });

Prefer curly braces

Use always curly braces, also for single line statements.

Not using curly braces to mark blocks can lead to subtle erros and introduces a new style (which readers will have to parse)


if (bla) {
  foo();
} else {
  fooToo();
}
          

Prefer quotes in object literals

Keywords or special characters require you to use quotes sometimes, so make code easier to read and to avoid errors prefer to always use single quotes like other strings.


var obj = {
  'foo': bar,
  'fooToo': 'string',
  'js is great': true
}