The **if** statement is the most fundamental control flow construct. Its condition is coerced to a boolean. **Falsy** values in JS: `false`, `0`, `''`, `null`, `undefined`, `NaN`. Everything else is **truthy**.
1Understanding if Statement
The if statement is the most fundamental control flow construct. Its condition is coerced to a boolean. Falsy values in JS: false, 0, '', null, undefined, NaN. Everything else is truthy.
Any value can be used as a condition — JS coerces it to boolean. Explicitly use Boolean() or !! when the intent isn't clear.
const temperature = 38;
if (temperature > 37.5) {
console.log('Fever detected!');
console.log('Please rest and hydrate.');
}2Practical Example
Here is a real-world application of if Statement showing how it is used in production JavaScript code.
// Truthy / Falsy values
if (0) { console.log('0 is truthy'); } // skipped
if ('') { console.log('empty is truthy'); } // skipped
if ([]) { console.log('[] is truthy'); } // runs!
if ({}) { console.log('{} is truthy'); } // runs!
if (null) { console.log('null is truthy'); } // skipped3Best Practices
Follow these guidelines when working with if Statement:
1. Always use curly braces even for single-line bodies
2. Check for null/undefined before accessing properties
3. Avoid assigning inside if conditions
Tip: Any value can be used as a condition — JS coerces it to boolean. Explicitly use Boolean() or !! when the intent isn't clear.
const temperature = 38;
if (temperature > 37.5) {
console.log('Fever detected!');
console.log('Please rest and hydrate.');
}