🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

if Statement

AI & DATA SCIENCE // if

The if statement executes a block of code only when a specified condition evaluates to true.

Syntax

if (condition) {
  // code to run when condition is true
}

Deep Dive Course

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.

editor.html
const temperature = 38;

if (temperature > 37.5) {
  console.log('Fever detected!');
  console.log('Please rest and hydrate.');
}
localhost:3000

2Practical Example

Here is a real-world application of if Statement showing how it is used in production JavaScript code.

editor.html
// 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'); } // skipped
localhost:3000

3Best 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.

editor.html
const temperature = 38;

if (temperature > 37.5) {
  console.log('Fever detected!');
  console.log('Please rest and hydrate.');
}
localhost:3000

Examples

Example 01Basic Usage
const temperature = 38;

if (temperature > 37.5) {
  console.log('Fever detected!');
  console.log('Please rest and hydrate.');
}
Example 02Advanced Example
// 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'); } // skipped

Best Practices

  • Always use curly braces even for single-line bodies
  • Check for null/undefined before accessing properties
  • Avoid assigning inside if conditions

Interview Question

What values are falsy in JavaScript?

Hint: There are exactly 6 falsy values.

The 6 falsy values are: false, 0, '' (empty string), null, undefined, and NaN. Every other value (including [], {}, 'false', -1) is truthy.

Exercises

MediumPractice using if Statement in a real scenario.
View Solution
const temperature = 38;

if (temperature > 37.5) {
  console.log('Fever detected!');
  console.log('Please rest and hydrate.');
}

Frequently Asked Questions

What values are falsy in JavaScript?

The 6 falsy values are: false, 0, '' (empty string), null, undefined, and NaN. Every other value (including [], {}, 'false', -1) is truthy.

Related Functions

elseConditionalsOperatorsswitch