Solve - True/False is not defined Error in JavaScript


Borislav Hadzhiev

Tue Oct 19 20212 min read

Solve - True/False is not defined Error #

The error "True / False" is not defined occurs when we misspell the booleans true or false. To solve the error, write the booleans in all lowercase, - true and false.

referenceerror true is not defined

Here's the code that caused the error.

const a = false; // ⛔️ ReferenceError: True is not defined // 👇️️ should be all lowercase `true` if (a === True) { console.log('success'); }

We misspelled the boolean value true, which caused the error.

This causes the JavaScript engine to look for a variable named True, which is doesn't find, so it throws the "ReferenceError: True is not defined".

To solve the "True / False" is not defined error, make sure to write the true and false booleans in all lowercase.

const a = false; if (a === true) { console.log('success'); }

The same is the case with the false boolean.

Here's how the code looks like when using the false boolean.

const a = false; // ⛔️ Should be `false` if (a === False) { console.log('success'); } // ✅ Do this instead if (a === false) { console.log('success'); }

If you're trying to compare to a string value, e.g. "TRUE" or "FALSE", you should enclose the value in quotes.

const a = 'TRUE'; if (a === 'TRUE') { console.log('a is equal to TRUE'); }

In the example, we check if the value stored in the a variable is equal to the string TRUE. If the condition is met, the if block is ran.

Note that comparisons like this are case sensitive. If you need to ignore case, use the toLowerCase() method on both strings.

const a = 'TRUE'; const b = 'true'; if (a.toLowerCase() === b.toLowerCase()) { console.log('a is equal to TRUE'); }

By converting both values to lowercase before comparing them we are able to ignore the case in the comparison.

Use the search field on my Home Page to filter through my more than 1,000 articles.