javaScript if statements and related concerns

In this post I will be writing about javaScript if statements, and other related concerns when working with conditionals in general when making a javaScript project. In javaScript there is also a conditional operator as well that can be used as a short hand for if else statements as well.

1 - javaScript if

A basic if statement in javaScript might look something like this.

1
2
3
4
5
6
7
var str = 'foo';
// with brackets
if (str === 'foo') {
console.log('bar');
}
// > 'bar'

it must start out with the if keyword followed by parenthesis, and within the parentheses must be and expression of anything that will evaluate to a true of false value of one kind of another. It does not have to be a boolean value, as just about any kind of value in javaScript has a true or false equivalent depending on the type and value of what is being evaluated.

2 - Some more basic examples

if statements can have brackets or not, and can also be used with an option else block as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var n = 42;
// with brackets
if (n === 42) {
console.log('the answer');
}
// > 'the answer'
// without
if (n >= 40)
console.log('the answer');
// > 'the answer'
if (n === '42') {
console.log('the answer is a string');
} else {
if (n === 42) {
console.log('the answer is a number.')
} else {
console.log('no answer');
}
}
// > 'the answer is a number'

3 - Else is not needed when making a function that returns something

If a function that is being made that is using the return keyword to return a result when called then else does not need to be used. The reason why is that return will stop any further execution of any additional code, so it can be used as a way to break out of a function. This differs from blocks of code where I might only want some code to run if and only if a condition is not met.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var isNeg = function (n) {
// if type is number and less than zero
if (typeof n === 'number' && n < 0) {
// return true
return true;
}
// if we get here return false
return false;
};
console.log(isNeg(NaN)); // false
console.log(isNeg('foo')); // false
console.log(isNeg('-1')); // false
console.log(isNeg(42)); // false
console.log(isNeg(-1)); // true

4 - Conditional operator

There is also a conditional operator in JavaScript this is an operator that takes three operands and returns one of two values depending on the boolean value of the left most operand before the question mark symbol that is used..

1
2
3
4
5
var obj = undefined;
obj = obj === undefined ? {} : obj;
console.log(typeof obj); // 'object'

Because the conditional operator is a kind of expression it can be used in conjunction with if statements.

1
2
3
4
5
6
7
8
9
10
11
12
var func = function (g) {
if (g = g < -25 ? false : true) {
return 'high';
} else {
return 'low';
}
};
console.log( func(0) ); // 'high'
console.log( func(10) ); // 'high'
console.log( func(-20) ); // 'high'
console.log( func(-32) ); // 'low'