Conditional Code

Contact Us or call 1-877-932-8228
Conditional Code

Conditional Code

Sometimes you only want to run a block of code under certain conditions. Flow control, via if and else blocks, lets you run code only under certain conditions.

Flow Control

var foo = true;
var bar = false;

// if statement
if (bar) {
	// this code will never run
	console.log('hello!');
}

// if ... else statement
if (bar) {
	// this code won't run
} else if (foo) {
	// this code will run
} else {
	// this code would run if foo and bar were both false
}

Note: While curly braces aren't strictly required around single-line if statements, using them consistently, even when they aren't strictly required, makes for vastly more readable code.

Be mindful not to define functions with the same name multiple times within separate if/else blocks, as doing so may not have the expected result.

Next