Skip to content

Latest commit

 

History

History
47 lines (32 loc) · 1.66 KB

javascript-style-rules.md

File metadata and controls

47 lines (32 loc) · 1.66 KB

JavaScript style rules

  • Use 'use strict'; in every Javscript file. This will help us write more secure Javascript. Declaring a variable without var is bad because the variable will be attached to global. See here for more information.

  • Always use var to declare a variable otherwise that variable will be attached to global and the scope in Javascript is tricky. So always declare with var.

  • Always end the line with ;. It's a best practice and issues can be caused if there isn't a ; at the end of the line. For example:

MyClass.prototype.myMethod = function() {
  return 42;
}  // No semicolon here.

(function() {
  // Some initialization code wrapped in a function to create a scope for locals.
})();
  • There are multiple ways to attach methods and properties to an object created via new, but Prototype is preferred here. If you want to know why, see here, here, and here.

  • Always use [] to create an array because of this.

new Array(2).length // returns 2
new Array(1).length // returns 1

Do this instead.

var a1 = [1];
a1.length; //returns 1

var a1 = [2];
a1.length //returns 1
  • Always use {} to create an object. Do this for readability.
var o = {};
  • Always use === or !== for strict comparison. Here's why.

References