ZetCode

JavaScript false keyword

last modified April 16, 2025

In this article we explore the false keyword in JavaScript. We'll cover its basic usage, comparison with other falsy values, and practical examples.

The false keyword

The false keyword is one of JavaScript's two boolean literals, representing the logical false value. It's used in conditional statements, comparisons, and boolean operations. false is a primitive value.

In JavaScript, values can be evaluated in a boolean context. Some values are "falsy" (evaluate to false), while others are "truthy". The false keyword is one of the six falsy values in JavaScript.

The other falsy values are 0, "" (empty string), null, undefined, and NaN. All other values are considered truthy. Understanding falsy values is crucial for writing effective conditionals.

Basic false usage

The simplest way to use false is in a boolean variable assignment.

main.js
let isLoggedIn = false;

if (isLoggedIn) {
    console.log("User is logged in");
} else {
    console.log("User is not logged in");
}

Here we declare a boolean variable isLoggedIn set to false. The if statement checks this value and executes the appropriate branch. Since false is falsy, the else block runs.

$ node main.js
User is not logged in

Comparing false with other falsy values

While false is falsy, not all falsy values are equal to false.

main.js
console.log(false == 0);        // true
console.log(false == "");       // true
console.log(false == null);     // false
console.log(false == undefined); // false
console.log(false === 0);       // false

This shows how false compares with other falsy values. The loose equality operator (==) performs type coercion, while strict equality (===) does not. Only 0 and empty string coerce to false.

$ node main.js
true
true
false
false
false

Using false in logical operations

false plays a specific role in logical AND and OR operations.

main.js
console.log(false && true);   // false
console.log(false || true);   // true
console.log(!false);          // true

let result = false && "Hello";
console.log(result);          // false

result = false || "Default";
console.log(result);          // "Default"

Logical AND returns the first falsy value or the last truthy value. Logical OR returns the first truthy value or the last falsy value. The NOT operator (!) inverts false to true.

$ node main.js
false
true
true
false
Default

False in conditional statements

false is often used directly in conditional statements for clarity.

main.js
function isAdult(age) {
    return age >= 18;
}

let age = 15;

if (isAdult(age) === false) {
    console.log("Access denied - too young");
}

// Alternative with !
if (!isAdult(age)) {
    console.log("Access denied - too young");
}

Both forms check for a false return value from the function. The first explicitly compares to false, while the second uses the NOT operator. Both are valid but serve different readability preferences.

$ node main.js
Access denied - too young
Access denied - too young

False as default parameter

false can be used as a default parameter value in functions.

main.js
function createUser(name, isAdmin = false) {
    console.log(`User ${name} created. Admin: ${isAdmin}`);
}

createUser("Alice");
createUser("Bob", true);
createUser("Charlie", false);

Here false serves as the default value for the isAdmin parameter. When not provided, it defaults to false. This pattern is common for boolean flags in functions.

$ node main.js
User Alice created. Admin: false
User Bob created. Admin: true
User Charlie created. Admin: false

False in object properties

Boolean properties often use false to indicate disabled or inactive states.

main.js
const settings = {
    darkMode: false,
    notifications: true,
    autoSave: false
};

if (!settings.darkMode) {
    console.log("Using light theme");
}

if (settings.notifications) {
    console.log("Notifications are enabled");
}

This example shows an object with boolean properties. The darkMode and autoSave are set to false, while notifications is true. We check these values to determine application behavior.

$ node main.js
Using light theme
Notifications are enabled

Practical use case: form validation

Here's a practical example using false in form validation.

main.js
function validateForm(data) {
    let isValid = true;
    
    if (!data.username) {
        console.log("Username is required");
        isValid = false;
    }
    
    if (data.password.length < 8) {
        console.log("Password too short");
        isValid = false;
    }
    
    return isValid;
}

const formData = {
    username: "",
    password: "123"
};

if (!validateForm(formData)) {
    console.log("Form validation failed");
}

This validation function starts with isValid = true and sets it to false if any validation fails. The function returns false if any checks fail, indicating invalid form data.

$ node main.js
Username is required
Password too short
Form validation failed

Source

Boolean literals - language reference

In this article we have demonstrated how to use the false keyword in JavaScript. We covered comparisons, logical operations, and practical applications.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all JavaScript tutorials.