Skip to content

Latest commit

 

History

History
57 lines (41 loc) · 1.47 KB

File metadata and controls

57 lines (41 loc) · 1.47 KB

JavaScript - New variables should contain null

All created, without any known values, new variables should contain null.
This rule helps to easily distinguish non-existent properties and variables from cleaned or already created but not have value.

Object.prototype.hasOwnProperty() cannot be conveniently applied to large objects.

It also helps maintain consistency with the server side.
In queries "undefined" and "null" have different meanings:

  • "undefined" - Value not set
  • "null" - The value is set as void

MDN Web Docs - null
eslint - init-declarations

Combination of:
eslint - no-undefined
eslint - no-undef-init

❌ BAD
let a;
const obj = {
    c: undefined,
};

console.log(a) // undefined
console.log(b) // undefined
console.log(obj.c) // undefined
console.log(obj.d) // undefined
✔ GOOD
let a = null;
const obj = {
    c: null,
};

console.log(a); // null
console.log(b); // undefined
console.log(obj.c); // null
console.log(obj.d); // undefined

Back to Code Guide - JavaScript

Back to Code Guide - Readme


Copyright © 2017 Stanislav Kochenkov