JavaScript: Boolean, Null, Undefined

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:00:50

JavaScript Booleans:

Booleans in JavaScript represent true or false values, used for logical operations and conditional statements.

Example:

let isTrue = true;
let isFalse = false;

The Boolean() Function:

The Boolean() function converts a value into a Boolean value, returning true for truthy values and false for falsy values.

Example:

let value = "Hello";
let isTruthy = Boolean(value); // true
let anotherValue = 0;
let isFalsy = Boolean(anotherValue); // false

Comparisons and Conditions:

JavaScript uses comparisons and conditions to determine the flow of a program based on Boolean results.

Example:

let x = 5;
let y = 10;
if (x < y) {
  console.log("x is less than y"); // Output: "x is less than y"
}

Everything With a "Value" is True:

In JavaScript, values that have data are considered truthy, even if they are not explicitly set to true.

Example:

let nonEmptyString = "Hello";
if (nonEmptyString) {
  console.log("This will be executed"); // Output: "This will be executed"
}

Everything Without a "Value" is False:

Values that are empty, null, undefined, 0, NaN, or an empty string are considered falsy in JavaScript.

Example:

let emptyString = "";
if (!emptyString) {
  console.log("This will be executed"); // Output: "This will be executed"
}

JavaScript Booleans as Objects:

JavaScript allows Booleans to be represented as objects, which can have properties and methods.

Example:

let booleanObj = new Boolean(true);
console.log(booleanObj.valueOf()); // Output: true 

JavaScript null:

null is a special value in JavaScript that represents the intentional absence of any object value.

Example:

let value = null;

JavaScript undefined:

undefined is a value that indicates a variable has been declared but not assigned a value or a property doesn't exist.

Example:

let x;
console.log(x); // Output: undefined

Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this