Skip to content

Latest commit

 

History

History
247 lines (135 loc) · 2.74 KB

variables.md

File metadata and controls

247 lines (135 loc) · 2.74 KB

Variables

enforce or disallow variable initializations at definition

❌ Disabled

// Bad
function bad() {
	let bar;
	let baz;
	bar = 3;
	baz = 4;
}

// Good
function good() {
	let bar = 1;
	let baz = 2;
	bar = 3;
	baz = 4;
}

disallow the catch clause parameter name being the same as a variable in the outer scope

❌ Disabled


disallow deletion of variables

✅ Enabled (error)

// Bad
/*
var x;
delete x;
*/

disallow labels that share a name with a variable

❌ Disabled


disallow specific globals

❌ Disabled


disallow shadowing of names such as arguments

✅ Enabled (error)

// Bad
/*
function NaN() {}

!function (Infinity) {};

var undefined;
*/

disallow declaration of variables already declared in the outer scope

✅ Enabled (error)

// Bad
/*
var a = 3;
function b() {
	var a = 10;
}
*/

// Good
var a = 3;
function b() {
	var c = 10;
}

disallow use of undefined when initializing variables

❌ Disabled

// Bad
var foo = undefined;
let bar = undefined;

disallow use of undeclared variables unless mentioned in a /*global */ block

✅ Enabled (error)

// Bad
/*
var a = someFunction();
b = 10;
*/

disallow use of undefined variable

❌ Disabled

// Bad
var foo = undefined;

disallow declaration of variables that are not used in the code

✅ Enabled (error)

// Bad
/*
var someUnusedVar = 42;
var x;
var y = 10;
y = 5;

var z = 0;
z = z + 1;
*/

disallow use of variables before they are defined

✅ Enabled (error)

// Bad
/*
alert(a);
var a = 10;

f();
function f() {}
*/

// Good
var a = 10;
alert(a);

function f() {}
f();